我有一个关于过渡的问题。
当我们想要转换到下一个UIViewController
时,我们会使用此代码:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
或
当我们想要使用此代码添加NavigationController
时:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
或
我们希望使用此代码将UIViewController
添加到另一个UIViewController
:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.view addSubview:secondViewController.view];
现在我想知道,如果我不在我的项目中使用xib
文件怎么办?
答案 0 :(得分:1)
您可以使用Storyboard中的ViewController
ViewController *controller = (ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];
或正常创建
// make Layout in viewController - viewDidLoad
ViewController *viewController = [[ViewController alloc] init];
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 64.0, 320.0, 24.0)];
label.textColor = [UIColor blackColor];
label.text = @"My text";
[self.view addSubview:label];
UIButton *button = ...;
[self.view addSubview:button];
}