UIAlertView 子视图定位

Posted

技术标签:

【中文标题】UIAlertView 子视图定位【英文标题】:UIAlertView subview positioning 【发布时间】:2012-05-21 07:37:31 【问题描述】:

我有一个 alertView,当用户更改标签时会弹出

UIAlertView *tempAlert = [[UIAlertView alloc] initWithTitle:@"Save Video?" 
                                                    message:@"Do you wish to save the video ?" 
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Save",nil];
[tempAlert show];
[tempAlert setDelegate:self];
self.saveAlert = tempAlert;
[tempAlert release];

现在我处理按钮单击事件,在此事件中,我想显示另一个带有 progressView 作为子视图的警报

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

if ([alertView.title isEqualToString:@"Save Video?"])

    if (buttonIndex == [alertView cancelButtonIndex]) 
    
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; //17.4.12
        // Adjust the indicator so it is up a few pixels from the bottom of the alert
        indicator.center = CGPointMake(self.saveAlert.bounds.size.width / 2, self.saveAlert.bounds.size.height - 40);
        [indicator startAnimating];
        [self.saveAlert addSubview:indicator];
        [indicator release];
        [(ImageCanvas1AppDelegate*)[[UIApplication sharedApplication] delegate] setMainAlert:self.saveAlert];


        [NSThread detachNewThreadSelector:@selector(performSaveOperationWithTabChangeToIndex:) toTarget:self withObject:[NSNumber numberWithInt:self.tab]];
    
    else
    
        [self performSelectorOnMainThread:@selector(playMovie)
                               withObject:nil waitUntilDone:YES];
    

else

    if (buttonIndex == [alertView cancelButtonIndex]) 
    
        [self.videoGenerator cancelVideoGeneration];
    


如果用户确实触摸了保存按钮,我会显示一个带有 progressView 作为子视图的 alertView 此方法在“playMovie”方法中调用

- (void)showAlert

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Generating Video" 
                                                message:@"Please wait!" 
                                               delegate:self cancelButtonTitle:@"Cancel" 
                                      otherButtonTitles:nil, nil];

[alert performSelectorOnMainThread:@selector(show)
                        withObject:nil
                     waitUntilDone:YES];
[alert setDelegate:self];
self.videoAlert = alert;
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
[progressView setFrame:CGRectMake(alert.bounds.size.width - progressView.bounds.size.width - 95,
                                  alert.bounds.size.height - progressView.bounds.size.height - 80, 
                                  progressView.bounds.size.width, 
                                  progressView.bounds.size.height)];
[alert addSubview:progressView];
self.progressView = progressView;
[progressView release];

[alert release];

[pool release];

问题是:

如果我正常显示alertView,子视图定位很好

但如果我在

中显示 alertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

progressView的位置不对

【问题讨论】:

【参考方案1】:

为什么要在 uialertview 中显示进度? UIalertviews 是模态的。制作您自己的视图并显示,这样您就可以完全控制,您可以添加暂停、继续和停止按钮,您可以显示您想要的所有信息。

【讨论】:

除非您想锁定应用程序,否则您不必制作模态视图。最简单的方法是制作一个 70% 透明的全屏视图,例如黑色背景。尺寸可以是您喜欢的任何尺寸。模态处理的问题是整个应用程序停止 这里是一个你可以很容易完成的例子,它可以让你轻松添加后台任务。简单得多。 code4app.com/photo/4f67fccc6803faaa75000001_1.png

以上是关于UIAlertView 子视图定位的主要内容,如果未能解决你的问题,请参考以下文章

将 UIImageview 添加到子视图后,无法单击 UIAlertView 中的按钮

在 iOS 7 SDK 中关闭 UIAlertview 后 UIView 没有响应

带有 UITextFields 和 UITextViews 的自定义 UIAlertView

iPhone App 中的 UIAlertView 动态

如何在 iPhone 的 UIAlertView 中添加 UITableView?

UIAlertView 替换