简体   繁体   中英

objective c iOS, reading from a text box ?

I'm new at objective c, I was playing with labels buttons and textboxes, I've a label, text box and two buttons(Add, Clear),

what I want to do is, write something in the textbox and when press to "add" button I want the text to appear on the label , and the clear button should clear label and the text box

I've ran the program and the build is succeed however when i write some text and press return button nothing happens, the virtual keyboard still stays there and text doesn't appear on the label, my codes are as follows:

in the .h file

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *label;
@property (retain, nonatomic) IBOutlet UITextField *txtbox;
@property (retain, nonatomic) IBOutlet UIButton *btnadd;
@property (retain, nonatomic) IBOutlet UIButton *btnclear;


- (IBAction)btnadd:(id)sender;
- (IBAction)btnclear:(id)sender;
- (IBAction)txtbox:(id)sender; 

in the .m file

@implementation ViewController
@synthesize label,txtbox,btnadd,btnclear;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [label release];
    [txtbox release];
    [btnadd release];
    [btnclear release];
    [super dealloc];
}
- (IBAction)btnadd:(id)sender {

    label.text = (txtbox.text);

}

- (IBAction)btnclear:(id)sender {
    txtbox.text=@"";
    label.text=@"";
}

- (IBAction)txtbox:(id)sender {
}
@end

I would really appreciate if someone helps, thanks in advance

Ps : what is the code to end the program if incase I add an exit button?

Thanks again,

Asim Gunduz

Have you linked everything correctly in your .xib/storyboard?

Read this on how to properly handle the return key.

You should not exit an app with a button. The only way to close your app should be pressing the home button. However, you could use

exit(0);

the code looks fine, but you can try these tutorials,

Linking the IBOutlet and IBAction

Hiding Keyboard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM