我是iOS编程新手,遇到了一个问题。连接在一起:AppDelegate,ViewController和XIB
做了什么:
我已经创建了iOS应用程序。 最初它有main.m,AppDelegate.h,AppDelegate.m和一些通常创建的其他支持文件(不包括代码)。
然后我手动创建带界面的XIB文件(LoginView.xib)并手动添加LoginViewController.h和LoginViewController.m来控制XIB。
将网点添加到LoginViewController.h。
所有我设置文件的所有者类(LoginViewController)和厦门国际银行和LoginViewController.h之间进行连接后。
问题描述:
我需要立即在应用程序的开始就展现实例化登录视图控制器和显示登录视图。
我尝试了几次尝试并阅读了一打论坛帖子,但没办法。除白色窗口背景外没有任何显示。 我该怎么做才对?
参考代码:
LoginViewController.h
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController
{
IBOutlet UIView *_loginView;
IBOutlet UITextField *_tfLogin;
IBOutlet UITextField *_tfPassword;
IBOutlet UIButton *_btnLoginToSystem;
}
- (IBAction)attemptLogin:(id)sender;
@end
LoginViewController.m
#import "LoginViewController.h"
@interface LoginViewController()
@end
@implementation LoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)attemptLogin:(id)sender
{
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) LoginViewController *loginViewController;
@end
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
[self.window.rootViewController presentModalViewController:self.loginViewController animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
更新! 伙计们,谢谢你们,我发现了另一个问题。一旦我得到你的批准,我的代码(在适当的版本,当然)是正确的,我开始模拟器和看到我的应用程序崩溃,但下列情况除外:
NSInternalInconsistencyException with the reason [UIViewController _loadViewFromNibNamed:bundle:] loaded the ... nib but the view outlet was not set.
感谢StackOverflow的我固定它。 Loaded nib but the view outlet was not set - new to InterfaceBuilder
报价从主题乔希司法评论我提供的链接:
- 打开XIB文件左边栏(顶部一个,长相上文件的所有者图标造成的问题
- 点击就像一个黄色轮廓框)
- 如果你没有看到右侧栏中,单击上方的工具栏中的“查看”第三个图标。这将显示右侧侧边栏
- 在右侧边栏中,单击第三个标签 - 看起来有点像报纸的标签
- 在顶部的“Custom Class”下,确保Class是应与此视图相对应的ViewController的名称。如果没有,请输入它
- 在右侧边栏中,单击最后一个选项卡 - 看起来像一个带箭头的圆形的选项卡
- 您应该在其下看到带有“view”的“outlets”。在拖动圆圈旁边的“查看”图标左边栏上(下一个,看起来像一个白色方形厚厚的灰色轮廓
- 保存XIB并重新运行
可能这些信息汇总到一个点将帮助其他新人通过我传递更快的方式。
伙计们,我的登录视图在启动后完美显示,我遇到了另一个问题:textfields不可编辑:(我点击textfield,将光标放在里面但不能输入字符(即使键盘没有显示)也不能改变焦点textfield。你有什么建议吗?谢谢! – DaddyM 2013-03-14 05:32:02
还有一个问题:我真的需要这个IBOutlet:IBOutlet UIView * _loginView; ????? – DaddyM 2013-03-14 05:43:23