Ios 当用户登录时打开新的视图控制器
关于stackoverflow,我问了所有类似的问题,但这些问题都很陈腐。从那时起,Xcode已经更新了多次,而答案对我来说并不适用 我对iOS开发非常陌生。我只想在用户登录后打开一个新的视图控制器 我只使用故事板。我没有.xib文件,因为我遵循了苹果的iOS教程Ios 当用户登录时打开新的视图控制器,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,关于stackoverflow,我问了所有类似的问题,但这些问题都很陈腐。从那时起,Xcode已经更新了多次,而答案对我来说并不适用 我对iOS开发非常陌生。我只想在用户登录后打开一个新的视图控制器 我只使用故事板。我没有.xib文件,因为我遵循了苹果的iOS教程 创建了单个视图项目。已删除原始视图控制器。(将此更改为LoginViewController,导致出现问题。) 添加了两个新的视图控制器:LoginViewController和MainMenuViewController。两者都嵌入
- (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和故事板完成此任务?在
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:UIResponder
@属性(强,非原子)UIWindow*window;
@属性(强)UIViewController*viewController;
@结束
在您的AppDelegate.m
文件中,您需要将下面的一行@synthesis viewController
添加到下面的@implementation
位置
在此之后,代码应该可以工作 在代码中发现了许多问题: 1) 当您使用故事板时,完全不需要在
应用程序中编写以下代码,而是使用选项来完成启动编写此代码是为了在您希望加载.xib文件并将ViewController作为初始ViewController时,以及在您不希望将序列图像板用作序列图像板时,将UINavigationController嵌入到ViewController中,这将被视为默认值
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文件中指定其-i操作,如下所示:
- (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.
}
}
照这个做,你就会成功。唯一的区别是Xcode版本和使用的segues。您应该使用Xcode6的序列,仅此而已,没有其他区别 您是否声明了@property(strong)UIViewController*viewController
在您的@界面中
?否。当用户成功登录时,我将显示新的viewcontroller(这是主菜单)。如果用户输入错误,它将保留在loginviewcontroller中。否,我应将其放置在何处。在我的例子中,它是@property(strong)UIViewController*LoginviewController代码>?您试图将其作为从appdelegate.m编写的类@implementation
的属性进行访问。我猜这个类的@interface
在一个名为appdelegate.h的文件中?把它放在@interface..
和@end
行之间的某个地方@结束
否则,对于UIResponder
,它说类“AppDelegate”的接口定义重复。你能检查一下我是否在正确的方法上使用了正确的viewcontroller名称吗?我觉得名称有点混乱。添加所有这些代码后,它甚至无法编译。不要创建类别。修改根@接口
。好的,我将添加到appdelegate.h中。我想名字应该是*LoginViewController
,对吗?而不是viewController
。因为我发现了一些人的问题,我不明白为什么投票会被否决。为了简单起见,只需将AppDelegate.h代码替换为我这里的代码。然后你的应用程序应该编译。好的,现在它可以编译了,但一旦提交,它会转到黑色背景的viewcontroller,导航控制器会指向登录页面。它未连接到我的主菜单viewcontroller。当前,我的故事板包含2个带有导航控制器的ViewController,但未相互连接。我需要创建segue来连接它们吗?没错。将segue from按钮拖动到MenuViewController。小心,不要使用不推荐使用的SEGUES。拖动到MainMenuViewController或其导航栏?我创建了模态segue。问题是现在它将转到mainmenucontroller,不管用户输入是对是错。每次我编译时,它都会变成黑色背景的空白页面,然后我需要单击导航按钮返回登录viewcontroller。拖动到MainViewController。在故事板中给segue一个标识符:“loginmainsgue”(它可以是您想要的任何东西)。对不起,我弄错了。在回答中再次检查我的-(iAction)GoToMainMenu:(id)sender方法。您需要在情节提要中实现performsguewithidentifier方法。仍然存在一些问题。我创建了-(iAction)makeConn
,并将此功能与情节提要上的提交按钮相连接。然后我在-(void)viewDidLoad
中调用了这个函数作为[self-makeConn]
。因此,基本上我的函数没有(id)sender
。这会引起问题吗?