我对ios开发非常新,我观看的许多教程都遵循xib方法,因为它们是在XCode 5之前录制的。我如何在单视图应用程序中使用xib方法?当我删除故事板并选择其中一个xib作为主界面时,它无法正常工作。谢谢你的提示。
答案 0 :(得分:11)
添加新文件,选择cococatouch并选择Objective -C类,然后转到下一个单击
你显示下面的屏幕
在底部必须为用户界面选择xib和下一个
AppDelegate.h:
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ViewController *viewObj;
UINavigationController *navObj;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewObj;
@property (strong, nonatomic) UINavigationController *navObj;
AppDelegate.m:
@synthesize viewObj,window,navObj;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];
window.rootViewController=navObj;
[window makeKeyAndVisible];
return YES;
}
答案 1 :(得分:7)
创建新项目时选择清空应用程序模板。
从左侧窗格中选择Cocoa touch - &gt; objective-c class - &gt;然后给出viewController的名称和Xib用户界面的tick选项。
然后在中在appDelegate文件中应用以下代码:
appDelegate.h文件:
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@interface HomeAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)HomeViewController *homeViewController;
@property (strong,nonatomic)UINavigationController *navigationController;
appDelegate.m文件:
- (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.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
@end
答案 2 :(得分:1)
您可以按照以下步骤执行此操作: -
在didFinishLaunchingWithOptions
方法
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
礼貌: - MKR
https://stackoverflow.com/a/19633059/1865424
检查一下,你错过了任何一步。