开发者

In Obj-c (xCode) how do I display this string to the text field?

开发者 https://www.devze.com 2023-02-18 08:40 出处:网络
I am trying to make a practice application that takes two entered words (word1, word2) and puts them together to make a compound word. I am very new to this and would like to know the correct way to d

I am trying to make a practice application that takes two entered words (word1, word2) and puts them together to make a compound word. I am very new to this and would like to know the correct way to display these two variables under the action "buttonPressed."

Here's the header file...

#import <UIKit/UIKit.h>

@interface Word_CombinerViewController : UIViewController {
    UITextField *word1;
    UITextField *word2;
    UITextField *display;
    UIButton *mashButton;

}
@property (nonatomic, retain) IBOutlet UITextField *word1;
@property (nonatomic, retain) IBOutlet UITextField *word2;
@property (nonatomic, retain) IBOutlet UITextField *display;
@property (nonatomic, retain) IBOutlet UIButton *mashButton;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)buttonPressed:(id)sender;


@end

And here's the implementation file (.m)...

#import "Word_CombinerViewController.h"

@implementation Word_CombinerViewController
@synthesize word1;
@synthesize word2;
@synthesize display;
@synthesize mashButton;


-(IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender {
    [word1 resignFirstResponder];
    [word2 resignFirstResponder];
}

-(IBAction)buttonPressed:(id)sender {
    NSString *newText = [NSString: @word1, @word2]
    display.text = newText;
    [newText release]

}

- (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 {
    [word1 release];
    [word2 release];
    [display release];
    [mashButton release];
    [super dealloc];
}

@end

I know this code is 开发者_运维知识库probably full of errors, but everyone has gotta start somewhere, right? Any feedback would be greatly appreciated, thank you!


NSString *newText = [NSString: @word1, @word2]

This code doesn't make any sense. The first part of a message is the receiver, and it won't have any colons in it. The second part is the message itself, i.e. the method name along with any necessary parameters. What you're looking for is:

NSString *newText = [NSString stringWithFormat:@"%@%@", word1, word2];

The -stringWithFormat: method uses a format string (very much like printf()) and a variable number of parameters to fill in the placeholders in the format string.

Also, when you create a string (or any object) using a "convenience method" like this, you shouldn't release it. You didn't retain or alloc or copy it, so it's not your responsibility to release it. You'll want to remove that line from your -buttonPressed method.


Try this:

-(IBAction)buttonPressed:(id)sender {
    NSString *newText = [word1.text stringByAppendingString:word2.text]
    display.text = newText;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消