一个简单的iPhone项目及代码
16lz
2021-12-04
版权所有,欢迎转载,转载请注明 : SinFrancis http://mdev.cc
iphone NIB文件 -> android xml布局文件
iphone delegate class + controller class -> android Activity
main方法 -> android的中AndroidManifest.xml要启动的Activity
步骤:
1. 使用XCODE创建项目
2. 打开nib文件,将需要的组件拖放至view中
3. 在Controller类中声明组件变量 \ 事件接收方法,注意需要 IBOutlet关键字,
4. 使用InterfaceBuilder链接Controller类中的变量\事件接收方法 至 每个组件上
5. 实现事件接收方法
(不用InterfaceBuilder依然可以实现UI界面,使用纯Code编写.)
main:
//// main.m// HelloUser//// Created by sin francis on 10-8-26.// Copyright __MyCompanyName__ 2010. All rights reserved.//#import <UIKit/UIKit.h>int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal;}
Delegate声明:
HelloUserAppDelegate.h
//// HelloUserAppDelegate.h// HelloUser//// Created by sin francis on 10-8-26.// Copyright __MyCompanyName__ 2010. All rights reserved.//#import <UIKit/UIKit.h>@class HelloUserViewController;@interface HelloUserAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; HelloUserViewController *viewController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet HelloUserViewController *viewController;@end
Delegata的实现:
HelloUserAppDelegate.m
//// HelloUserAppDelegate.m// HelloUser//// Created by sin francis on 10-8-26.// Copyright __MyCompanyName__ 2010. All rights reserved.//#import "HelloUserAppDelegate.h"#import "HelloUserViewController.h"#import "MyNewsViewController.h"@implementation HelloUserAppDelegate@synthesize window;@synthesize viewController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after app launch [window addSubview:viewController.view]; [window makeKeyAndVisible];return YES;}- (void)dealloc { [viewController release]; [window release]; [super dealloc];}@end
Controller的声明:
//// HelloUserViewController.h// HelloUser//// Created by sin francis on 10-8-26.// Copyright __MyCompanyName__ 2010. All rights reserved.//#import <UIKit/UIKit.h>@interface HelloUserViewController : UIViewController {//链接到HelloUserViewController.xib 界面中的display label组件IBOutlet UILabel *displayLable;//链接到HelloUserViewController.xib 界面中的name Field组件IBOutlet UITextField *nameField;//链接到HelloUserViewController.xib 界面中的Button组件IBOutlet UIButton *sayButton;//注意:以上的只是引用,并未真实的链接到组件上去,那么就需要我们使用Interface Builder进行链接//1. 使用Interface Builder打开HelloUserViewController.xib,打开HelloUserViewController.xib中View//2. 右键单击 或者CTRL+左键单击 "File's Owner",可以在弹出的Outlet一栏中看见 //displayLable nameField sayButton三个变量名称//3. 分别拖动变量右边的空心圈连接至View界面上的组件即可}//定义一个按钮事件接收器//如何链接此接收器到button上呢//1. 使用Interface Builder打开HelloUserViewController.xib,打开HelloUserViewController.xib中View//2. 右键单击 或者CTRL+左键单击 "File's Owner",在Received Actions一栏中可以看到sayHello的动作//3. 拖动sayHello右边的空心圈至View界面上的按钮即可//4. 实现你的sayHello方法吧-(IBAction) sayHello : (id) sender;@end
Contoller的实现:
//// HelloUserViewController.m// HelloUser//// Created by sin francis on 10-8-26.// Copyright __MyCompanyName__ 2010. All rights reserved.//#import "HelloUserViewController.h"@implementation HelloUserViewController/*// The designated initializer. Override to perform setup that is required before the view is loaded.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization } return self;}*//*// Implement loadView to create a view hierarchy programmatically, without using a nib.- (void)loadView {}*//*// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad { [super viewDidLoad];}*//*// Override to allow orientations other than the default portrait orientation.- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait);}*///sayHello的实现方法-(void) sayHello:(id)sender{NSString *userName = nameField.text;NSString *helomessage = [[NSString alloc] initWithFormat:@"Hello,%@",userName];displayLable.text=helomessage;[helomessage release];nameField.text=NULL;}- (void)didReceiveMemoryWarning {// Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];// Release any cached data, images, etc that aren't in use.}- (void)viewDidUnload {// Release any retained subviews of the main view.// e.g. self.myOutlet = nil;}- (void)dealloc { [super dealloc];}@end
实例代码下载:
更多相关文章
- 链接器解析多重定义的全局变量
- 开机引导程序只执行一次的方法
- android mtk6592 禁止拖动主界面图标
- Android(安卓)Activity界面切换添加动画特效
- Android(安卓)自定义组件 方法篇
- Windows环境下Android(安卓)Studio系列4—界面介绍
- Android(安卓)Interface(UI) 界面控件简单介绍01
- Android四大组件的理解
- TextView