问题描述
我尝试在故事板上调用主 ViewController
。在我的应用程序中,还有一个 .h
, .m
文件,没有xib或故事板。
I try to call the main ViewController
on my storyboard. In my app there is a additional .h
, .m
file with no xib or storyboard.
在这个.m文件中,我有一个按钮:
In this .m file T craeted a button:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(home:)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
NSLog(@"Home-Button line 645");
此按钮应链接到Storyboard中的主ViewController。该视图具有标识符HauptMenu。我没有错误,但视图没有改变到我的主ViewController。有什么问题?
This button should link to my main ViewController in the Storyboard. The view has the identifier HauptMenu. I got no error, but the view doesnt change to my main ViewController. What is wrong?
- (IBAction)home:(id)sender {
NSLog(@"Button was tapped");
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
NSLog(@"1");
[viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
NSLog(@"2");
[self.navigationController pushViewController:viewController animated:NO];
[viewController release];
NSLog(@"3");
}
推荐答案
如果你的话 .m
文件与任何故事板无关,不会 self.storyboard
是无
?
If your .m
file is not associated with any storyboard, wouldn't self.storyboard
be Nil
?
尝试:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
确保将 storyboardWithName:
更改为无论你的故事板是什么名字。
Make sure to change the storyboardWithName:
to whatever your storyboard is named.
你可能没有任何错误,因为Objective-C的处理方式与其他语言不同,如果你尝试在nil上调用一个方法,它(通常)不会抛出异常,它只返回nil。以下代码将很乐意运行,不会抛出任何编译器或运行时错误:
You may not have gotten any errors because Objective-C handles nil differently than other languages, it (usually) won't throw an exception if you try to call a method on nil, it will just return nil. The following code will happily run, throwing no compiler or runtime errors:
UIViewController * test = nil;
[test viewDidLoad];
NSString * storyBoardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
storyBoardName = @"MainStoryboard_iPad";
} else {
storyBoardName = @"MainStoryboard_iPhone";
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
storyBoardName bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
这篇关于UiButton / IBAction - 从一个RootView链接到Storyboard中的mainView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!