gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:08:36 34 4
gpt4 key购买 nike

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

做了什么:

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

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

向 LoginViewController.h 添加了 socket 。

毕竟我设置了文件的所有者类 (LoginViewController) 并在 XIB 和 LoginViewController.h 之间建立了连接。

问题描述:

我需要显示实例化登录 View Controller 并在应用程序启动时立即显示登录 View 。

我尝试了几次并阅读了十几个论坛主题,但没有办法。除了白色窗口背景外,没有显示任何内容。我该如何纠正?

引用代码:

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

登录 View Controller .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

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

大概这些信息聚合成一个点,会帮助其他新人更快通过我走过的路。

最佳答案

在 application:didFinishLaunchingWithOptions: 方法中将 LoginViewController 设置为 Root View Controller 。

self.window.rootViewController = self.loginViewController;

删除这一行:

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

关于ios - 连接在一起 : AppDelegate, ViewController 和 XIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396947/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com