IOS xib生成界面和代码生成界面两种方式混合
应用程序代理类
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(, , , );
_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 == ) {
subAlterView.message = @"下次小心点";
}
else if (buttionIndex == ){
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 xib生成界面和代码生成界面两种方式混合的更多相关文章
- 【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系
(1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器能够自己定义不同的状态栏例如以下: -(BOOL)prefersStatusBa ...
- Docker镜像构建的两种方式
关于Docker里面的几个主要概念 这里用个不太恰当的比方来说明. 大家肯定安装过ghost系统,镜像就像是ghost文件,容器就像是ghost系统.你可以拿别人的ghost文件安装系统(使用镜像运行 ...
- ios 实现跳转到评价界面的两种方式
要想在App内跳转到特定App的详情页或者评论页,首先需要获取到App的id.在 iTunes Connect网站上登陆之后,选择“我的App”,然后点击某个特定的App进入,在App信息的综合信息中 ...
- iOS打包为ipa的两种方式和生成P12证书的方式
iOS项目打包为ipa的两种方式: 准备工作:先行在Xcode里面打开preferences,填写apple id. 通过iTunes+Xcode 在Xcode里,把模拟器调整为iOS Device, ...
- centos安装图形界面通常有两种方式
centos安装图形界面通常有两种方式 1.通过系统安装,在配置选择软件界面,选择GNOME桌面模式.
- 两种方式实现java生成Excel
Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...
- ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
最近做项目遇到生成二维码的问题,发现网上用的最多的是ThoughtWorks.QRCode和QrCode.Net两种方式.访问官网看着例子写了两个Demo,使用过程中发现两个都挺好用的,Thought ...
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- maven生成war包的两种方式
war包即对WEB应用程序进行打包,用于应用容器的部署.如在jboss中只要把war包丢入deploy目录下即可发布自己的应用了.打包方式有很多中,很多工具本身就支持此功能.下面主要介绍通过maven ...
随机推荐
- Android Support v4,v7,v13
Android Support v4 是最早(2011年4月份)实现的库.用在Android1.6 (API lever 4)或者更高版本之上.它包含了相对V4, V13大的多的功能. 例如:Frag ...
- Ghost版Win8.1系统企业版下载
host版Win8.1系统企业版,下载完成后一定要使用校验工具验证GHO文件MD5值,如果不符请不要安装,不然安装失败后果自负.GHO文件路径一定不要带中文,否则无法安装.安装完成第一次进入桌面会黑屏 ...
- css笔记——关于 body/html 的高度百分比
body{ height:100%; 视窗的高度 min-height:100%;文档的具体高度} 这两个百分比的具体高度在页脚永远放在文档底部非常重要,此时用min-height:100% 具体 ...
- jCallout 实现气泡提示
在提交表单前.焦点转移后或者 keyup 时往往需要对输入的文本就行检验,如果输入内容不符合相关约定则要进行提示或警告,有一个叫 jCallout 的插件可以轻松实现该功能,该插件基于 jQuery ...
- C++记录2
1, 求成员变量的偏移: 2, const实现机制:在编译期间完成,对于内置类型,如int, 编译器可能使用常数直接替换掉对此变量的引用.而对于结构体不一定. 编译器在优化代码时把j直接优化成64h了 ...
- Junit的最简单样例:Hello world!
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3824934.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- PerformSelector may cause a leak because its selector is unknown 解决方法
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3801030.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- mysql 主从同步 Last_SQL_Error
参考文章: http://kerry.blog.51cto.com/172631/277414/ http://hancang2010.blog.163.com/blog/static/1824602 ...
- 《APUE》第6章笔记
这一章主要介绍了口令文件和组文件的结构和一些围绕这些结构的函数. 口令文件即passwd就是在/etc/passwd中可以查阅.其结构是: 上图四个平台能支持的就用黑点表示. 因为加密口令这一项放在p ...
- 一些达成共识的JavaScript编码风格约定
如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...