2012-07-10 34 views
4

我有iPhone和我想要的时候AlertView显示和用户按确定按钮后,该视图应该改变,但它没有发生。如何移动到下一个视图控制器后显示alertview

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    return; 
    [self moveToView]; 

-(void)moveToView 
{ 
    MainViewController*targetController=[[MainViewController alloc]init]; 
    [self.navigationController pushViewController:targetController animated:YES]; 
} 
+0

您使用的故事板? – 2012-07-10 10:29:58

回答

7

请使用UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // Code to move to next view 
    [self moveToView]; 
} 

注意:实现你的接口声明的UIAlertViewDelegate。同时声明UIAlertView将代理设置为self。

希望这可以帮助你。

+0

似乎不正确。应该实现接口的['didDismissWithButtonIndex'](https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView :didDismissWithButtonIndex :)方法而不是['clickedButtonAtIndex'](https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:clickedButtonAtIndex :)。 – 2014-05-06 01:50:41

3

那么,你正在设置自己作为UIAlertView的代表。这是正确的,这是你必须采取的第一步。在此之后继续前进,实现这个方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    [self moveToView]; 
} 

你也可以做一个switch语句这里查看按下了哪个按钮。但由于AlertView上只有OK按钮,因此没有必要。

希望这会有所帮助。

干杯!

+0

不,不正确。看[这个答案](http://stackoverflow.com/a/11411404/642706)。 – 2014-05-06 02:00:30

2

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法中实现您的代码,并在您的.h文件中添加UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
     NSLog(@"The index: %d",buttonIndex); 
     MainViewController*targetController=[[MainViewController alloc]init]; 
     [self.navigationController pushViewController:targetController animated:YES]; 
     [targetController release]; 
    } 

我认为这会对您有所帮助。

6

它很简单。实现UIAlertViewDelegate并将代码放在那里。

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    MainViewController*targetController=[[MainViewController alloc]init]; 
    [self.navigationController pushViewController:targetController animated:YES]; 
    [targetController release]; 
} 
+0

这应该是被接受的答案。当调用'didDismissWithButtonIndex'时,对话框(UIAlertView)消失,因此您可以安全地将视图推送到导航控制器。[不正确的接受的答案](http://stackoverflow.com/a/11411381/642706)使用'clickedButtonAtIndex'来推送视图会导致不好的事情发生,从视觉上没有任何事情发生(旧的iOS)到导致异常的事件到崩溃(iOS 7)。 – 2014-05-06 01:56:18

5

在.H,实现UIAlertViewDelegate

@interface ClassName : ParentClassName <UIAlertViewDelegate> 
在.M

,添加这种方法,

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

     [self moveToView]; 

} 
+0

不,试图在UIAlertView仍在屏幕上时(在'clickedButtonAtIndex'期间)推送视图是不好的。每[此答案]使用'didDismissWithButtonIndex'(http://stackoverflow.com/a/11411404/642706)。 – 2014-05-06 01:59:50

4

实现警报视图的委托:

在你yourClass.h文件:

@interface yourClass : UIViewController<UIAlertViewDelegate>{ 

} 
@end 

在你yourClass.m文件:

@implementation yourClass 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    [self moveToView]; 
} 
@end 
相关问题