Change iOS tab view from alert

I have a tabbed application with 2 tab views. I want to change the view from the second tab to the first when the user presses "OK" on an alert view.

The following code snippet works when I use it on an -IBAction button pressed, but it doesn't do anything when I use it inside the alert code:

self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];

Why does it not work when used as below, in my SeondViewController.m in my

- (void)viewDidLoad {
[super viewDidLoad];?

UIAlertView *alert =[[UIAlertView alloc]
initWithTitle:@"Camera and Target coincident"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];

I'm just learning this, so anything you can offer would be helpful.

When I use the following code: if ([ theProjection Trans_Initialise] == 1) { NSString * msg = nil; UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show];

   - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
       if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"OK"]) {
           self.tabBarController.selectedIndex = 0;
       }
   }

I get the error message on -(void) alertView "Invalid argument type void to unary expression" Am I doing something grammatical, or is it (quite possibly) something I just didn't understand?


First of all, you need a delegate call back from the alert view informing you about the button that was clicked.

Your alert view should be created like this :

UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

Then in your m file implement the function :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"OK"]) {
        self.tabBarController.selectedIndex = 0;
    }

}
链接地址: http://www.djcxy.com/p/67256.html

上一篇: 在应用程序激活时接收远程通知

下一篇: 从警报更改iOS选项卡视图