IPhone Development Tips(2)First Simple Sample

本文详细介绍如何使用Xcode创建iOS应用程序的基本交互元素,包括为按钮添加操作、创建IBOutlet连接到文本框和标签,以及实现文本框代理方法来处理键盘输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IPhone Development Tips(2)First Simple Sample

2. Build Action for Pages
Add action for the button
select 'MainStoryboard.storyboard', change to 'Assistant Editor' at the right top.
select the file hello3ViewController.h in Assistant

select the button and push 'ctrl' on our keyboard, we drag our mouse between the
@interface hello3ViewController : UIViewController

@end

We get a window. We will configure some items as following:
Connection ----> Action
Name ----> greeting
Type --> id
Event --> Touch Up Inside
Arguments ---> Sender

click 'connect', the xcode will add interface in .h file, and implementation in .m file.

Create Outlets for the Text Field and the Label
When you create an outlet connection in Xcode, the connection is archived in the storyboard file and restored when the app runs. The restored connection allows the two objects to communicate with each other at runtime.

create the outlet to textField, the related window shows these items:
Connection ---> Outlet
Name ----> textName
Type ----> UITextField
Storage ---> Weak ------------>> Click 'Connect' after enter all these values.

add method in .m file:
- (void) viewDidUnload
{
[self setTextName:nil];
[super viewDidUnload];
self.textname = nil;
}

Add outlet to label. And we can see the connections information after we click the 'connection inspector' button at the right top.

Make the Text Field's Delegate Connection
push ctrl and select text field and drag to the right yellow button with name 'Hello3 View Controller'.
release mouse and select delegate.

Implementing the View Controller
Add a Property for the User's Name
In the file hello3ViewController.h
@interface hello3ViewController:
UIViewController{
NSString *userName;
}
...
@property (nonatomic,copy) NSString *userName;
@end

In the file hello3ViewController.m
@implementation hello3ViewController

@synthesize textName = _textName;
@synthesize labelGreeting = _labelGreeting;
@synthesize userName = _userName;

...
- (IBAction)greeting:(id)sender {
self.userName = self.textName.text;
NSString *nameString = self.userName;
if([nameString lenght] == 0){
nameString = @"World";
}
NSString *greeting = [[NSString alloc]
initWithFormat:@"hello, %@!",nameString];
self.labelGreeting.text = greeting;
}

Configure the View Controller as the Text Field's Delegate
In an iOS app, the keyboard is shown automatically when an element that allows text entry becomes the first responder, it is dismissed automatically when the element loses first responder status.

select file hello3ViewController.m file
...snip...
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField{
if(theTextField == self.textName){
[theTextField resignFirstResponder];
}
return YES;
}
...
@end

select the .h file and add some to the @interface line:
@interface hello3ViewController : UIViewController <UITextFieldDelegate>{
...snip...

error message:
unrecognized selector sent to instance ....

solution:
I link the method input to textfield, but I did not delete that.

references:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iPhone101/Articles/05_ConfiguringView.html
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html
http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/ios_development_workflow/000-Introduction/introduction.html
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/MobileHIG/Introduction/Introduction.html
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值