按钮单击移动到 iOS 中的另一个视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19573185/
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
Moving to another view controller in iOS on Button Click
提问by christianleroy
I am very new to iOS and I am still trying to grasp things. I am trying out this simple program in XCode 5 where when a user clicks the button, he will be redirected into another view controller. I did what other forums told other askers to do, but I seem to encounter an error.
我对 iOS 很陌生,我仍在努力掌握事物。我正在 XCode 5 中尝试这个简单的程序,当用户单击按钮时,他将被重定向到另一个视图控制器。我做了其他论坛告诉其他提问者做的事情,但我似乎遇到了错误。
Here are my codes:
这是我的代码:
In ViewController.m:
在 ViewController.m 中:
- (IBAction)button1:(id)sender {
WebServiceViewController *wc = [[WebServiceViewController alloc]
initWithNibName:@"WebServiceViewController"
bundle:nil];
[self.navigationController pushViewController:wc animated:YES];
}
I placed an import "WebServiceViewController.h" in the headers.
我在标题中放置了一个导入“WebServiceViewController.h”。
Here is the error (not an IDE error message, but not a success either):
这是错误(不是 IDE 错误消息,但也不是成功):
Here is the exception:
这是例外:
2013-10-25 02:05:51.904 sample[16318:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/Guest/Library/Application Support/iPhone Simulator/7.0/Applications/C6FA1F33-5E11-40C2-8C69-DA368F21CA5F/sample.app> (loaded)' with name 'WebServiceViewController''
*** First throw call stack:
(
0 CoreFoundation 0x017345e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b78b6 objc_exception_throw + 44
2 CoreFoundation 0x017343bb +[NSException raise:format:] + 139
3 UIKit 0x004ca65c -[UINib instantiateWithOwner:options:] + 951
4 UIKit 0x0033cc95 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
5 UIKit 0x0033d43d -[UIViewController loadView] + 302
6 UIKit 0x0033d73e -[UIViewController loadViewIfRequired] + 78
7 UIKit 0x0033dc44 -[UIViewController view] + 35
8 UIKit 0x00357a72 -[UINavigationController _startCustomTransition:] + 778
9 UIKit 0x00364757 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
10 UIKit 0x00365349 -[UINavigationController __viewWillLayoutSubviews] + 57
11 UIKit 0x0049e39d -[UILayoutContainerView layoutSubviews] + 213
12 UIKit 0x00294dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13 libobjc.A.dylib 0x014c981f -[NSObject performSelector:withObject:] + 70
14 QuartzCore 0x03aee72a -[CALayer layoutSublayers] + 148
15 QuartzCore 0x03ae2514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16 QuartzCore 0x03ae2380 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17 QuartzCore 0x03a4a156 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
18 QuartzCore 0x03a4b4e1 _ZN2CA11Transaction6commitEv + 393
19 QuartzCore 0x03a4bbb4 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
20 CoreFoundation 0x016fc53e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
21 CoreFoundation 0x016fc48f __CFRunLoopDoObservers + 399
22 CoreFoundation 0x016da3b4 __CFRunLoopRun + 1076
23 CoreFoundation 0x016d9b33 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x016d994b CFRunLoopRunInMode + 123
25 GraphicsServices 0x036859d7 GSEventRunModal + 192
26 GraphicsServices 0x036857fe GSEventRun + 104
27 UIKit 0x0022a94b UIApplicationMain + 1225
28 sample 0x00002e7d main + 141
29 libdyld.dylib 0x01d70725 start + 0
30 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Furthermore, I'm using storyboard. I have an initial scene where the user logs in. This scene has a navigation controller embedded. After logging in, I'm not gonna use segue since there would be several view controllers that can be loaded, depending on what type of user has logged in. I'm trying to do this simple program since I'm gonna be using it more complexly in the future.
此外,我正在使用故事板。我有一个用户登录的初始场景。这个场景嵌入了一个导航控制器。登录后,我不会使用 segue,因为会有几个视图控制器可以加载,具体取决于登录的用户类型。我正在尝试执行这个简单的程序,因为我要使用它将来会更复杂。
I'm obviously missing something. I hope someone can help me. I'd be very grateful.
我显然错过了一些东西。我希望有一个人可以帮助我。我会很感激。
EDIT:
编辑:
This worked for me
这对我有用
WebServiceViewController *wc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]
instantiateViewControllerWithIdentifier:@"WebServiceViewController"];
[self.navigationController pushViewController:wc animated:YES];
storyboardWithName depends on the storyboard's name.
storyboardWithName 取决于故事板的名称。
回答by ChikabuZ
try this:
尝试这个:
WebServiceViewController *wc = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"WebServiceViewController"];
also set storyboardId to WebServiceViewController in Identity Inspector
还在 Identity Inspector 中将 storyboardId 设置为 WebServiceViewController
回答by uvesten
You know, using a storyboard you can also define a segue that isn't connected directly to a button, name that segue, and then call it programatically like so:
您知道,使用情节提要还可以定义不直接连接到按钮的转场,命名该转场,然后像这样以编程方式调用它:
[self performSegueWithIdentifier:@"showWebServiceViewController" sender:nil];
I think this approach is cleaner and more in line with the storyboard way of working.
我认为这种方法更简洁,更符合故事板的工作方式。
Of course, depending on which user has logged in you could perform different named segues to get to the approporiate screen.
当然,根据登录的用户,您可以执行不同的命名转场以进入适当的屏幕。