我在stackoverflow上经历了所有类似的问题,但问题非常陈旧。从那时起Xcode已经多次更新,答案对我不起作用。
我对iOS开发非常陌生。我只想在用户登录后打开一个新的视图控制器。
我只使用故事板。我没有.xib文件,因为我遵循了Apple的iOS教程。
创建了一个视图项目。删除了原始视图控制器。 (将此更改为LoginViewController,导致出现问题。)
添加了两个新的视图控制器:LoginViewController和MainMenuViewController。两者都嵌入了导航控制器。我还将LoginViewController设置为故事板的初始视图控制器。
根据这篇文章添加了以下代码:
(Navigation Controller Push View Controller)
在LoginViewController.m中:
- (IBAction)GoToMainMenu:(id)sender
{
MainMenuViewController* controller = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
}
在AppDelegate.m中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"LoginViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
在上面的代码中,它说:
在“AppDelegate”类型的对象上找不到self.viewController
我的问题是如何使用Xcode 6.0.1和故事板完成此任务?
答案 0 :(得分:1)
在您的代码中发现了许多问题:
1)当您使用故事板时,application didFinishLaunchingWithOptions
中的代码绝对无需。 正在编写此代码以将UINavigationController嵌入到ViewController,当您要加载.xib文件并将viewController作为初始viewcontroller时,以及当您不想使用storyboard时,故事板将被视为默认。
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"LoginViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController
alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
您正在使用上面的代码加载.xib文件,不存在。删除上面的代码并设置故事板,因为您正在使用故事板。
推送viewcontroller:
在您的情况下,按一下按钮。在LoginViewController.h文件中给它-IBAction这样:
- (IBAction)GoToMainMenu:(id)sender;
现在在您的LoginViewController.m文件中实现此方法:
- (IBAction)GoToMainMenu:(id)sender
{
if(check login success)
{
[self performSegueWithIdentifier:@"loginMainSegue" sender:self];
}
else
{
You can set Alert here saying invalid login.
}
}
按照SIMPLE STORYBOARD TUTORIAL进行操作即可。唯一的区别将是Xcode版本和使用的segues。您应该使用Xcode6的segues,而不是其他区别。
答案 1 :(得分:0)
在AppDelegate.h
文件中,您需要声明UIViewController *viewController
。以下是AppDelegate.h
文件的外观示例。
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong) UIViewController *viewController;
@end
在AppDelegate.m
文件中,您需要在其下方@synthesize viewController
处添加以下行@implementation
。
在此之后,代码应该可以工作。