[英]How to programatically change views in iOS
In Xcode 4.5 you can change views from a segue, But I want to programatically change views in Mainstoryboard. 在Xcode 4.5中,您可以从segue更改视图,但我想以编程方式更改Mainstoryboard中的视图。 I Know in Xib.
我知道在Xib。 we use this method:
我们使用这种方法:
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
I want to change the view when the user clicks done in the textfield (This code detects if user clicks done): 我想在用户在文本字段中单击完成时更改视图(此代码检测用户是否单击完成):
-(IBAction)hidekeyboard {
[sender resignFirstResponder];
}
You create a segue in your storyboard that goes from the view that has the text field in question and goes to your SecondViewController
. 您在故事板中创建了一个segue,它来自具有相关文本字段的视图,并转到
SecondViewController
。 You have to assign an identifier to it (also in the storyboard, click on the line connecting the two view controllers), let's call it "segueToSecondViewController". 你必须为它分配一个标识符(也在故事板中,单击连接两个视图控制器的线),让我们称之为“segueToSecondViewController”。 Then, you can manually call the segue using:
然后,您可以使用以下方法手动调用segue:
[self.storyboard performSegueWithIdentifier:@"segueToSecondViewController" sender:self];
An alternative to segueing is to instantiate the viewController from the storyboard. segueing的替代方法是从故事板中实例化viewController。
First you assign a Storyboard ID to the viewController in the storyboard. 首先,将Storyboard ID分配给storyboard中的viewController。
Then you instantiate that viewController, and present it. 然后,您实例化该viewController,并呈现它。
MBTagEditorViewController *tagEditor = [self.storyboard instantiateViewControllerWithIdentifier:@"TagEditor"];
[self presentModalViewController:tagEditor animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.