您的位置:首页 > 移动开发 > IOS开发

iOS开发学习笔记 -- (三)动态创建视图

2013-03-04 15:48 686 查看
这次的例子会演示用编程的方式使用TabBarController及创建视图。

它会创建两个TAB,然后在每个TAB页中,加载我们自己定义的一个视图。最终结果如下:





先新建一个项目,因为不需要interface builder,我们选择Empty的项目类型。

项目创建完成后,主要文件只有:AppDelegate.h和AppDelegate.m。

接下来,我们需要创建自定义的视图。在Xcode中,选择:New File -- Objective-C class,在接下来的对话框中,输入类名:MyViewController,并且选择subclass为UIViewController,因为我们不需要interface builder,需要确保最下面的"With XIB for user interface“没有被选中。

生成的MyViewController.h代码如下:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController

@end


MyViewController.m代码:

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (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.
}

@end
我们需要在视图中添加自定义的控件,为了简单,我们就显示一个Label好了。

我们在viewDidLoad方法中添加下面的代码:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 40)];
[label setText: [NSString stringWithFormat:@"Hello, this is view %d", i++]];
[self.view addSubview:label];


这里面的i是一个全局变量,在MyViewController的最头上定义:

@implementation MyViewController
static int i=1;
这样,我们的视图就能够根据创建的顺序,动态地显示:Hello, this is view 1, 或view 2, view3...,取决于创建的视图的个数。

视图创建好了,我们需要使用TabBarControl了。同样使用代码来创建,这次,我们个性AppDelegate.m中的didFinishLaunchingWithOptions方法,修改后代码如下(注意需要在最顶端 #import"MyViewController.h"):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
tabBarController = [[UITabBarController alloc]init];
NSMutableArray *tabs = [[NSMutableArray alloc] initWithCapacity:2];

MyViewController *myViewController1 = [[MyViewController alloc] init];
[myViewController1 setTitle:@"View1"];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:myViewController1];
[tabs addObject:navController1];
navController1.tabBarItem.image = [UIImage imageNamed:@"star.png"];

MyViewController *myViewController2 = [[MyViewController alloc] init];
[myViewController2 setTitle:@"View2"];
UINavigationController *navController2= [[UINavigationController alloc] initWithRootViewController:myViewController2];
navController2.tabBarItem.image = [UIImage imageNamed:@"star.png"];
[tabs addObject:navController2];
[tabBarController setViewControllers:tabs];

tabBarController.selectedIndex = 0;
self.window.rootViewController = tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;}


先创建TabBarController的实例。tabs数组用于存放tab列表。接下来我们分别创建了两个MyViewController的实例,并且将它们添加到了两个对应的UINavigationController中,设置UINavigationController的按钮图片,最后将UINavigationController对象添加到tabs数组中。

也就是说,tabs数组包含的是两个UINavigationController对象,而这两个对象本身又分别包含了我们自定义的MyViewController对象。

接下来,将tabs数组设置为tabBarController对象的viewControllers,设置当前window的rootViewController。

这里为什么要用UINavigationController对象呢,我们看上面的最后实现效果,两个View最上方分别都有一个标题:View1, View2,这就是这个控件的作用。实际上完全可以不用这个控件,实现代码就会变成类似:

[tabs addObject:myViewController1];
当然,最后就没有标题了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐