开发者

Errors with Objective-C with Name Strings and Conditionals

To start, I'm very new to Objective-C so please be specific. I've been having the same 2 errors for a while now, I'm making an application in Xcode that allows you to insert your name, then randomly selects a number, and each number has a sentence attached to it that will put your name at the end. After the conditionals, I keep having the same error where the text field should retur开发者_Python百科n. Here is a part of my code:

@implementation MyViewController

@synthesize textField;
@synthesize label;
@synthesize string;

- (IBAction)changeGreeting:(id)sender {

self.string = textField.text;

// Allows the user to input their name and what happens if they don't put anything
- (IBAction)changeGreeting:(id)sender {
    self.string = textField.text;
    NSString *nameString = string;
    if ([nameString length] == 0) {
        nameString = @"no name";
    }   
// Defines a random variable between 0 and 5 and says what happens when each is selected
    int r = arc4random() % 6;

    if (r == 0) {
        label.text = [[NSString alloc] initWithFormat:@"You will become employee of the month... at McDonalds, %@!", nameString];
    }
    else if (r == 1) {
        label.text = [[NSString alloc] initWithFormat:@"I wouldn't call yours a future, more of a dissapointment, %@!", nameString];
    }       

    else if (r == 2) {
        label.text = [[NSString alloc] initWithFormat:@"I hope you like your parents' couch because that is the bulk of your future, %@!", nameString];
    }

    else if (r == 3) {
        label.text = [[NSString alloc] initWithFormat:@"Your future entails a jail cell, bad choices, and a toilet you'd never want to sit on, %@!", nameString];
    }

    else if (r == 4) { 
        label.text = [[NSString alloc] initWithFormat:@"You will write a famous book about how bad this app is, %@!", nameString];
    }

    else if (r == 5) {
        label.text = [[NSString alloc] initWithFormat:@"Your future involves cats, and lots of them, %@!", nameString];
    }

}
// Returns the text field
    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
        if (theTextField == textField) {
            [textField resignFirstResponder];
        }
        return YES;
    }
    - (void)dealloc {
        [textField release];
        [label release];
        [string release];
        [super dealloc];
    }

@end

My errors are that the changeGreeting is undeclared, there is an expected ; before the : and there's an expected declaration at the end of the input.


You need to close the changeGreeting method, i.e. a } after the else if. Currently the UITextFieldDelegate protocol method (textFieldShouldReturn) is 'within' changeGreeting.


Try

- (IBAction)changeGreeting:(id)sender {
    self.string = textField.text;
    NSString *nameString = string;
    if ([nameString length] == 0) {
        nameString = @"no name";
    }
    int r = arc4random() % 2;
    if (r == 0) {
        label.text = [[NSString alloc] initWithFormat:@"You will become employee of the month... at McDonalds, %@!", nameString];
    }
    else if (r == 1) {
        label.text = [[NSString alloc] initWithFormat:@"I wouldn't call yours a future, more of a dissapointment, %@!", nameString];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == textField) {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)dealloc {
    [textField release];
    [label release];
    [string release];
    [super dealloc];
}


First of all why is there two - (IBAction)changeGreeting:(id)sender? And

- (IBAction)changeGreeting:(id)sender {

self.string = textField.text;

should have }

- (IBAction)changeGreeting:(id)sender {

self.string = textField.text;

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜