`
sillycat
  • 浏览: 2564573 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

IPhone Development Tips(2)First Simple Sample

阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics