Ошибки с Objective-C со строками имен и условными выражениями - PullRequest
1 голос
/ 08 марта 2011

Для начала, я очень плохо знаком с Objective-C, поэтому, пожалуйста, будьте конкретны. У меня были те же 2 ошибки в течение некоторого времени, я делаю приложение в XCode, которое позволяет вам вставить свое имя, затем случайным образом выбирает число, и к каждому числу прикрепляется предложение, которое будет содержать ваше имя в конце. После условных выражений у меня продолжает появляться та же ошибка, когда текстовое поле должно возвращаться. Вот часть моего кода:

@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

Мои ошибки в том, что changeGreeting не объявлено, есть ожидаемое; перед: и есть ожидаемое объявление в конце ввода.

Ответы [ 3 ]

2 голосов
/ 08 марта 2011

Вам необходимо закрыть метод changeGreeting, то есть a} после else if.В настоящее время метод протокола UITextFieldDelegate (textFieldShouldReturn) находится в пределах changeGreeting.

0 голосов
/ 21 марта 2011

Прежде всего, почему существует два - (IBAction) changeGreeting: (id) отправитель? И

- (IBAction)changeGreeting:(id)sender {

self.string = textField.text;

должно иметь }

- (IBAction)changeGreeting:(id)sender {

self.string = textField.text;

}
0 голосов
/ 08 марта 2011

Попробуйте

- (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];
}
...