微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 连接在一起:AppDelegate,ViewController和XIB

我是iOS编程的新手,遇到了一个问题.

做了什么:

我已经创建了iOS应用程序.
最初它有main.m,AppDelegate.h,AppDelegate.m和一些其他通常创建的支持文件(不带代码).

然后我手动创建带接口(LoginView.xib)的XIB文件,并手动添加LoginViewController.h和LoginViewController.m来控制XIB.

为LoginViewController.h添加了插座.

毕竟我设置了文件的所有者类(LoginViewController)并在XIB和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;
}

UPDATE!
伙计们,多亏你们,我发现了另一个问题.一旦我批准我的代码(当然在适当的版本之后)是正确的,我启动了模拟器并看到我的应用程序崩溃,但有以下异常:

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

Josh Justice的评论从我提供的主题链接到:

  1. Open the XIB file causing problems
  2. Click on file’s owner icon on the left bar (top one,looks like a yellow outlined Box)
  3. If you don’t see the right-hand sidebar,click on the third icon above “view” in your toolbar. This will show the right-hand sidebar
  4. In the right-hand sidebar,click on the third tab–the one that looks a bit like a newspaper
  5. Under “Custom Class” at the top,make sure Class is the name of the ViewController that should correspond to this view. If not,enter it
  6. In the right-hand sidebar,click on the last tab–the one that looks like a circle with an arrow in it
  7. You should see “outlets” with “view” under it. Drag the circle next to it over to the “view” icon on the left bar (bottom one,looks like a white square with a thick gray outline
  8. Save the xib and re-run

将这些信息汇总到一个点可能会帮助其他新手以更快的速度通过.

解决方法

在您的应用程序中将LoginViewController设置为根视图控制器:didFinishLaunchingWithOptions:方法.
self.window.rootViewController = self.loginViewController;

删除此行:

[self.window.rootViewController presentModalViewController:self.loginViewController animated:NO];

原文地址:https://www.jb51.cc/iOS/334707.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐