views:

507

answers:

2

I have an iPhone app that uses an action sheet but I can't work out how to make one of the buttons open a new view when pressed. I know how to implement the action sheet - that's not a problem, its the actual action of opening the new view that's the issue.

Any help would be greatly appreciated. Thanks.

A: 

In the UIAlertView's -didDismissWithButtonIndex, instantiate your new view controller and push it onto the navigation stack:

- (void)alertView:(UIAlertView *)alertView 
                 didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NewViewController *controller = [[NewViewController alloc] init];
        [[self navigationController] pushViewController:controller animated:YES];
        [controller release], controller = nil;
    }
}
Matt Long
Did the OP mention using a navigation controller?
Chris Long
You mean in the `UIAlertViewDelegate`, you shouldn't subclass `UIAlertView` really.
notnoop
Yep. Missed that one completely. Alert View... Action Sheet.. it's all the same. ;-) Sorry to the OP. And yes, notnoop, I wasn't subclassing. That's the UIAlertViewDelegate.
Matt Long
+1  A: 

I usually just create a new method and have the action sheet do it.

For instance:

switch (buttonIndex) {
    case 0:
        [self openModalView];
        break;
 }

Then, in your openModalView method:

- (void)openModalView {
    MyModalViewController *myController = [[MyModalViewController alloc] init];
    [self presentModalViewController:myController animated:YES];
    [myController release];
}
Chris Long
Thank you so much - I'd spent hours trying to work this out. Your solution worked brilliantly.
Graeme