xcode 在 Xcodes Storyboard 中单击按钮时加载视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13894099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load view when button is clicked in Xcodes Storyboard
提问by nimrod
I just started to use Storyboard in Xcode. I have a starting view that has some buttons inside. Is there a way to load a special view when a button is clicked?
我刚开始在 Xcode 中使用 Storyboard。我有一个起始视图,里面有一些按钮。有没有办法在单击按钮时加载特殊视图?
I only found this workaround:
我只找到了这个解决方法:
-(IBAction)loadRegistration:(id)sender {
// load registration controller
UIStoryboard *storyboard = self.storyboard;
RegisterController *svc = [storyboard instantiateViewControllerWithIdentifier:@"RegisterController"];
[self presentViewController:svc animated:YES completion:nil];
}
回答by rdelmar
You don't need any code to load a new view. You just control-drag from the button to the controller you want to present, and choose the segue type. If the controller with the button is in a navigation controller you can choose push or modal, if it's not, then you want to choose modal.
您不需要任何代码来加载新视图。您只需从按钮按住 Control 拖动到要呈现的控制器,然后选择转场类型。如果带有按钮的控制器在导航控制器中,您可以选择推送或模态,如果不是,则您要选择模态。
回答by Levi
The way you did it seems ok. You can also connect the current View Controller to the one you want to present with a segue in the Interface Builder (ctrl + drag). When the button is tapped, you would call [self performSegueWithIdentifier:@"segue identifier"];
. Of course, you set the id of the segue in the IB.
Even more simpler would be to connect the button with the new View Controller in IB (also ctrl+drag). This way you won't even need the IBAction.
你这样做的方式似乎没问题。您还可以将当前的视图控制器连接到要在界面生成器中使用 segue 呈现的视图控制器(ctrl + 拖动)。当点击按钮时,您将调用[self performSegueWithIdentifier:@"segue identifier"];
。当然,你在IB中设置segue的id。更简单的是将按钮与 IB 中的新视图控制器连接起来(也是 ctrl+drag)。这样你甚至不需要IBAction。