我有一个关于过渡的问题。
当我们想要转换到下一个 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
文件怎么办???
最佳答案
你可以使用 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];
}
关于ios - 如何在没有 xib 文件的情况下使用 uiviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25282889/