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

iOS Programming 学习笔记 - 01 View and ViewController

2015-02-06 23:53 429 查看

1. UIView

NIB文件的名称需要和ViewController的文件名一致,这样在初始化该view controller时,使用如下方法即可,否则需要制定NIB文件和Bundle:

BNRReminderViewController *rvc = [[BNRReminderViewController alloc] init];
否则需要显示指定:

// This will get a pointer to an object that represents the app bundle
NSBundle *appBundle = [NSBundle mainBundle];

// Look in the appBundle for the file BNRReminderViewcontroller.xib
BNRReminderViewController *rvc = [[BNRReminderViewController alloc] initWithNibName:@"BNRReminderViewController" bundle:appBundle];


2. 注册LocalNotification

#ifdef __IPHONE_8_0 //这里主要是针对iOS 8.0,相应的8.1,8.2等版本各程序员可自行发挥,如果苹果以后推出更高版本还不会使用这个注册方式就不得而知了……
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}  else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
#else
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif


3. UIViewController

下面是和ViewControlle相关的一些常用的方法:

application:didFinishLaunchingWithOptions: 初始化和设置root view controller的地方,应用程序启动时被调用,直到应用程序结束,这个方法只会被调用一次。

initWithNibName:bundle: 用于初始化ViewController。

loadView: 重载这个函数可以通过编程的方式为view controller创建view。

viewDidLoad: 重载这个函数来对view进行配置,这个函数在controller的view被创建时被调用。

viewWillAppear: 每当view要被显示到屏幕上时被调用。

在view controller的loadView方法中,加载对应的view

- (void)loadView
{
// Create a view
BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init];

// Set it as *the* view of this view controller
self.view = backgroundView;
}


4. UITabBarController

分四步,在appDelegate的didFinishLaunchingWithOptions方法中创建3个view controller;创建tabbar controller;将view controller传递给tabbar controller;将tabbar controller设置为rootViewController

BNRHypnosisViewController *hvc = [[BNRHypnosisViewController alloc] init];

BNRReminderViewController *rvc = [[BNRReminderViewController alloc] init];

BNRQuizViewController *qvc = [[BNRQuizViewController alloc] init];

UITabBarController *tabBarcontroller = [[UITabBarController alloc] init];
tabBarcontroller.viewControllers = @[hvc, rvc, qvc];

self.window.rootViewController = tabBarcontroller;
在每个viewController中重载initWithNibName方法,设置tab item的名称和图片

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self)
{
// Set the tab bar item's title
self.tabBarItem.title = @"Hpnosis";

// Create a UIImage from a file
// This will use Hypno@2x.png on retina display devices
UIImage *i = [UIImage imageNamed:@"Hypno"];

// Put that image on the tab bar item
self.tabBarItem.image = i;
}

return self;
}
程序执行效果如下:



5. UIScrollView

首先创建绘图区CGRect screenRect,大小于屏幕大小相等,然后创建一个宽度是屏幕大小两倍的bigRect。
接下来创建一个UIScrollView,大小为screenRect,将其加入window的sub view数组中。
创建一个自定view - hypnosis,大小为screenRect,将其加入scroll view的sub view数组中。
再创建第二个自定义view - hypnosis,大小为screen大小,但是x坐标的起始为前一个view的宽度,将其加入scroll view的sub view数组中。
使能paging功能。
设置scroll view的contentSize为bigRect。

// Create CGRects for frames
CGRect screenRect = self.window.bounds;
CGRect bigRect = screenRect;
bigRect.size.width *= 2.0;

// Create a screen-sized scroll view and add it to the window
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
[self.window addSubview:scrollView];

// Create a screen-sized hypnosis view and add it to the scroll view
BNRHypnosisView *hypnosisView = [[BNRHypnosisView alloc] initWithFrame:screenRect];
[scrollView addSubview:hypnosisView];

// Add a second screen-sized hypnosis view just off screen to the right
screenRect.origin.x += screenRect.size.width;
BNRHypnosisView *anotherView = [[BNRHypnosisView alloc] initWithFrame:screenRect];
[scrollView addSubview:anotherView];
scrollView.pagingEnabled = YES;

// Tell the scroll view how big its content area is
scrollView.contentSize = bigRect.size;
程序执行效果如下,可以看到屏幕下发有一个滚动条,横向拖动滚动条可以在2个view之间进行切换。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: