问题
I have an application using Storyboard. On a view there is an AlertViewDialog.
When the user clicks the first button ("Yes"), how can I open an other view on the Storyboard?
回答1:
my be this can help :
- Drag a View in Then go to Identity Inspector (Shortcut: option+apple+3).
- Select the newly dragged View and give unique name from identify inspector in title Storyboard ID . // see the image for reference
create SecondViewController class (.h &.m) subclass of viewController .
then from alert view code (as you said when YES is clicked )
paste the code mentioned below
SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"vinay"];
[svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:svc animated:YES completion:nil];
let me know if any issues occur.
回答2:
May this helps:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ClassNameViewController *viewController = (ClassNameViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewIdentifierOnStoryboard"];
[self presentModalViewController:viewController animated:NO];
回答3:
The first thing you need to do is set the UIAlertView
delegate to do this add UIAlertViewDelegate
to your @interface
so it looks like
@interface myClass : super <UIAlertViewDelegate>
// super could be anything like `UIViewController`, etc
@end
and in the @implementation
you can then added something like
@implementation myClass
........... Some code
- (IBAction)someActionMethod:(id)sender
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil
message:@"Would you like to move on?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[myAlertView show];
// [myAlertView release]; Only if you aren't using ARC
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex) {
case 1:
SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
[svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
// [self presentViewController:svc animated:YES]; // Deprecated in iOS 6.0
[self presentViewController:svc animated:YES completion:nil]; // Introduced in iOS 5.0
break;
default:
break;
}
}
@end
Remember to set the unique identifier in the storyboard. You can do this by going to your .storyboard
and in the Identity Inspector (Selecting the third one in) you can set the Storyboard ID
this is what you will need to match with what is in instantiateViewControllerWithIdentifier
so in the case above it would be "secondViewController"
. It is that simple.
Remember to dismiss this view when you are done you will need to use
[self dismissModalViewControllerAnimated:YES];
The above has actually been deprecated in iOS 6.0 but you can use
[self dismissModalViewControllerAnimated:YES completion:nil];
Which does the same thing except it adds a completion block on the end.
Hope this helps.
来源:https://stackoverflow.com/questions/14381418/open-storyboard-view-manually