IOS xib生成界面和代码生成界面两种方式混合
2013-09-19 01:09
288 查看
应用程序代理类
WKAppDelegate.m
xib生成界面和代码生成界面两种方式混合发现的问题:
1.在init方法里面代码生成的界面元素无法显示在控制器的View上;
2.重载loadView方法会造成死循环。
故把用代码生成界面元素放在(控制器的)viewDidLoad中。
WKViewController.h
WKViewController.m
不明白的是Xcode自动生成的WKViewController.m中又对WKViewController声明了一次
@interface WKViewController ()
@end
类WKViewController不是在头文件WKViewController.h中已经声明过了吗?!!!真搞不懂。
WKAppDelegate.m
// // WKAppDelegate.m // HelloWorld // // Created by easy5 on 13-9-18. // Copyright (c) 2013年 easy5. All rights reserved. // #import "WKAppDelegate.h" #import "WKViewController.h" @implementation WKAppDelegate - (void)dealloc { [_window release]; [_viewController release]; [super dealloc]; } //应用程序加载完毕以后调用 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. //传入xib文件初始化控制器 self.viewController = [[[WKViewController alloc] initWithNibName:@"WKViewController" bundle:nil] autorelease]; //self.window.rootViewController = self.viewController; //已经执行了 //[self.window addSubview:self.viewController.view] self.window.rootViewController = self.viewController; //show the main window //让窗口成为主窗口,只有主窗口才能与用户进行交互 [self.window makeKeyAndVisible]; return YES; } //应用程序失去焦点时调用 - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } //应用程序进入后台(点击Home键) - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } //应用程序进入前台 - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } //应用程序程序获取焦点(获取焦点后才能跟用户进行交互) - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } //当程序被关闭 - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
xib生成界面和代码生成界面两种方式混合发现的问题:
1.在init方法里面代码生成的界面元素无法显示在控制器的View上;
2.重载loadView方法会造成死循环。
故把用代码生成界面元素放在(控制器的)viewDidLoad中。
WKViewController.h
// // WKViewController.h // HelloWorld // // Created by easy5 on 13-9-18. // Copyright (c) 2013年 easy5. All rights reserved. // #import <UIKit/UIKit.h> #import <UIKit/UIButton.h> @interface WKViewController : UIViewController{ //不从xib生成空件 UIButton *_endWorldButton; UIAlertView *_endWorldAlterView; } //UI界面不用管理内存,所以用assign //如果用retain,就得自己 release //IBOutlet让属性出现在xib文件中 @property(nonatomic, assign) IBOutlet UITextField *qq; @property(nonatomic, assign) IBOutlet UITextField *pwd; //等价于- (void)login; //IBAction就void,而且让方法出现在xib文件中 - (IBAction)login; @end
WKViewController.m
不明白的是Xcode自动生成的WKViewController.m中又对WKViewController声明了一次
@interface WKViewController ()
@end
类WKViewController不是在头文件WKViewController.h中已经声明过了吗?!!!真搞不懂。
// // WKViewController.m // HelloWorld // // Created by easy5 on 13-9-18. // Copyright (c) 2013年 easy5. All rights reserved. // #import "WKViewController.h" @interface WKViewController () @end @implementation WKViewController //- (id) init { // self = [super init]; // if (nil != self) { //} // return self; //} - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _endWorldButton = [UIButton buttonWithType:UIButtonTypeInfoDark]; CGRect _endBtnFrame = CGRectMake(25, 200, 100, 50); _endWorldButton.frame = _endBtnFrame; [self.view addSubview:_endWorldButton]; [_endWorldButton addTarget:self action:@selector(endWorldClick:) forControlEvents:UIControlEventTouchUpInside]; } - (void)endWorldClick:(id)sender{ UIButton *button = (UIButton *)sender; if (button == _endWorldButton) { NSLog(@"End World!"); _endWorldAlterView = [[UIAlertView alloc] initWithTitle:@"end world now" message:@"Really?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"OK", nil]; [_endWorldAlterView addButtonWithTitle:@"wuliao1"]; [_endWorldAlterView addButtonWithTitle:@"wuliao2"]; [_endWorldAlterView addButtonWithTitle:@"wuliao3"]; [_endWorldAlterView show]; } } - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttionIndex { NSLog(@"clicked %i", buttionIndex); if (alertView == _endWorldAlterView) { UIAlertView *subAlterView = [[UIAlertView alloc] initWithTitle:@"Which you had choiced" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; if (buttionIndex == 0) { subAlterView.message = @"下次小心点"; } else if (buttionIndex == 1){ subAlterView.message = @"OMG! 你真炸了"; } else { subAlterView.message = @"你TMD真无聊!"; } [subAlterView show]; [subAlterView release]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)login{ NSLog(@"hello world!"); NSLog(@"QQ=%@, PWD=%@", _qq.text, _pwd.text); [self.view endEditing:YES]; } - (void) dealloc { [_endWorldButton dealloc]; [_endWorldAlterView release]; [super dealloc]; } @end
相关文章推荐
- 大钟的ios开发之旅(4)————简单谈谈ios程序界面实现的三种方式(代码创建,xib和storyboard)
- IOS成长之路-使用xib界面与和代码相关联的方法
- 【代码笔记】iOS-json文件的两种解析方式
- iOS 加载xib的两种方式
- IOS成长之路-使用xib界面与和代码相关联的方法
- android启动第一个界面时即闪屏的核心代码(两种方式)
- IOS UI 代码界面跳转常用方式
- iOS进阶之xib上控件自动生成纯代码
- Yii shell生成代码的两种方式
- 【代码笔记】iOS-向服务器传JSON数据的两种方式
- iOS 关于使用xib创建cell的两种初始化方式
- 模型和自定义控件的两种方式(代码和xib)
- ios的两种界面跳转方式
- ios的两种界面跳转方式
- 将ios代码在后台(子线程)执行的两种简单方式
- 【代码笔记】iOS-向服务器传JSON数据的两种方式
- iOS打包为ipa的两种方式和生成P12证书的方式
- iOS中读取Cell的两种方式(资源包种加载Cell,从nib文件中注册cell)和xib知识点
- 【代码笔记】iOS-判断中英文混合的字符长度的两种方法