2015-05-22 23 views
0

在此輸入代碼我在提示提醒時無法返回到先前的視圖控制器。在提醒後打開新的視圖控制器

我想要做的是讓用戶輸入數據,然後出現一條警告,說它已成功,然後返回到前一個視圖控制器。

我目前還沒有代碼這樣做,並正在尋求與我應該把什麼幫助。你的UIAlertView中UIAlertviewController的

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
[enterAlert show]; 

使用委託方法

- (IBAction)saveLabel:(id)sender 
{ 
    NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"DATA"]; 
    NSMutableArray *currentDataArray; 
    if (data == nil) 
    { 
     currentDataArray = [[NSMutableArray alloc]init]; 
    } 
    else 
    { 
     currentDataArray = [[NSMutableArray alloc]initWithArray:data]; 
    } 
    [currentDataArray addObject:self.textField.text]; 
    [[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:@"DATA"]; 
} 

- (IBAction)enterButtonPressed:(id)sender 
{ 
    NSLog(@"enterButtonPressed"); 
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
    [enterAlert show]; 
} 

回答

1

//如果您使用解僱的號碼

[self dismissViewControllerAnimated:YES completion:^{ 
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
    [enterAlert show]; 
}]; 

//如果u [R使用導航,popViewController

[CATransaction begin]; 
[CATransaction setCompletionBlock:^{ 
    // handle completion here 
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
    [enterAlert show]; 
}]; 

[self.navigationController popViewControllerAnimated:YES]; 

[CATransaction commit]; 
+0

這很有用,謝謝! 你能告訴我CATransaction做了什麼嗎? –

+0

https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CATransaction_class/index.html – Mukesh

0

集委託自我。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex==0){ 
     // Do your Stuff Here.... 
     [self.navigationController popViewControllerAnimated:TRUE]; 
    } 
} 
+0

他的要求是不this.Please閱讀問題 – Mukesh

+0

我會在哪裏聲明我想改變的視圖控制器? 這似乎並沒有帶我回到主屏幕 –

0

添加以下UIAlertViewDelegate的方法來實現文件中:

- (void)alertView:(UIAlertView *)alertView 
     didDismissWithButtonIndex:(NSInteger)buttonIndex { 
     // If you are presenting this view controller 
    [self dismissViewControllerAnimated:YES completion:nil]; 

    // If you are pushing this view controller 
    [self.navigationController popViewControllerAnimated:YES]; 
    } 

還記得你的UIAlertView中委託設爲您的視圖控制器,切換到下面的代碼:

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
+0

你可以提供給我更多的信息,第一部分的代碼應該去? –

+0

一旦用戶點擊UIAlertView內的「確定」按鈕,您是否想要回到先前的視圖控制器?或者你想在屏幕上顯示UIAlertView節目,並同時返回到前一個視圖控制器?我懷疑後者是可行的。 –

+0

一旦用戶按下「OK」,我想要轉到上一個視圖控制器 –

0
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 
enterAlert.tag=100; 
[enterAlert show]; 
} 
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
if (alertView.tag == 100) { 
    if (buttonIndex == 0) { 
     // Do something when ok pressed 
    // If you are presenting this view controller 
[self dismissViewControllerAnimated:YES completion:nil]; 

// If you are pushing this view controller 
[self.navigationController popViewControllerAnimated:YES]; 
    } else { 
     // Do something for other alertviewButton} 

else{// Do something with responses from other alertViews by giving tags 
} 
相關問題