自定义函数出错

时间:2012-04-20 16:06:34

标签: objective-c xcode ios5 xcode4.3

我正在尝试使用UILabel.text来采用NSString自定义函数,但是却被错误所困扰。我仍然是iOS的新手,但我的直觉表明它与语法有关。

- (void)viewDidLoad
{
    ...

    //Creating scrollable view
    CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
    [self.view addSubview:scrollView];

    CGSize scrollViewContentSize = CGSizeMake(320, 800);
    [scrollView setContentSize:scrollViewContentSize];

    //Inserting Title
    UILabel *title = [[UILabel alloc]init];
    [title setFrame:CGRectMake(0, 0, 300, 40)];
    title.backgroundColor = [UIColor clearColor];
    title.text = self.setQuestion;<-error
    [scrollView addSubview:title];
    [title release];
}
- (void) setQuestion{
    NSString *qn = [NSString stringWithFormat:@"What do you get with%@", 5];

}

提前完成了......

1 个答案:

答案 0 :(得分:2)

title.text = [self setQuestion];

但是setQuestion编码不正确。应该是:

- (NSString*) setQuestion{
    return [NSString stringWithFormat:@"What do you get with %d", 5];
}

不要忘记将setQuestion的原型包含在.h文件中。

(嗯,实际上,格式字符串编码也不正确。应该是“你用%d得到什么”。)