views:

2296

answers:

4

I am running Objective-C/Cocoa in Shell in Windows, not on Mac OS X.

Now, I want to append a string with an existing string in a text field when I click a button. I've created a button in a window, but I don't know how to append a string when I click that button.

How can I append a string to a text field when that button is clicked?

+9  A: 

This is a pretty empty question and will probably get deleted quickly. However, to attempt to answer it in some way:

If you want to display something when you click a button then you'll need a UIButton (already added you have said) and let's say a UILabel. You can find a UILabel in the same window you discovered the button. Add it to the view, somewhere just below the button.

You then want to find the .h file that corresponds to the .xib file (if you set something up by default in XCode it will have been created for you). Let's call it MyViewController.h although it will have a different name in your app.

In that .h file, you want to create an IBAction (so that you can detect the button click in code) and an IBOutlet (so you can update the text on the label).

The code should end up looking something like this:

//
//  MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {
    UILabel *label;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
- (IBAction)buttonClick;

@end

Then, go back to the .xib file where you created your button. Select "File's Owner", open up the "Identity inspector" under the "Tools" menu and make sure the class is set to the same name as the class whose .h file we were just editing.

Now we need to connect the button and the label. Click on the button and bring up the "connections inspector" from the "Tools" menu again. Click in the circle next to "touch up inside" and drag it to the "File's owner". Select the "buttonClick" method that pops up in a window for you. Now select the File's owner. Click in the circle next to the word "label" and drag it to the label on your iPhone screen. This has now connected up the label and the button to the code you wrote before.

Finally, save the .xib file and open up the MyViewController.m file (or whatever it is called in your app). Add the following to it:

//
//  MyViewController.m

#import "MyViewController.h"   // Swap this name for the name of your .h file

@implementation MyViewController

@synthesize label;

-(IBAction)buttonClick {
    label.text = @"Hello World!"
}

- (void)dealloc {
    [label release];
    [super dealloc];
}

@end

Spend some time looking at various tutorials etc and you'll quickly get up to speed.

h4xxr
+1  A: 

Short answer: create an IBAction method in a controller class and set the IBAction as the target of your button.

Long answer: Get a book and learn the details of Cocoa. Cocoa is difficult to get started with, but once you've made the initial investment you'll be able to quickly create apps. I'd recommend Cocoa Programming for Mac OS X by Aaron Hillegass. It's desktop orientated rather than iPhone, but that shouldn't put you off.

There are also loads of great web resource to learn Cocoa. Cocoa Dev Central and the Apple developer site are great places to start.

Benedict Cohen
+1  A: 

You didn't specify whether you're using UILabel (Cocoa Touch) or NSTextField (Cocoa). For the latter, get the field's stringValue, send that object a stringByAppendingString: message with the string you want to append, and pass the result to the field's setStringValue: method.

Peter Hosey
A: 
import <UIKit/UiKit.h>
@interface MyViewController : UIViewController {
    UIButton *btnToAppend;
    UITextField *textfield;
}

- (IBAction)buttonClick:(id)sender;

@end

import "MyViewController.h"

@implementation MyViewController

-(void)init{
    btnToAppend = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btbToAppend.frame = CGRectMake(10, 10, 100, 30);
    [btnToAppend setText:@"Append Text" forState:UIControlStateNormal];
    [btnToAppend addTarget:self action:@select(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubView:btnToAppend];
    textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubView:textfield];
}

-(IBAction)buttonClick:(id)sender {
    NSString *string = [textfield.text stringByAppendingString:@"Welcome "];
    textfield.text = string;
}

- (void)dealloc {
    [super dealloc];
}

@end

Output will be like :

Welcome Welcome Welcome ...

jfalexvijay