2013-11-24 187 views
0

我有这个小项目可以帮助我适应Objective-C和iOS开发,它应该在屏幕上显示引号并每隔20秒或任何时间更改引号用户按下按钮。为了实现这个目标我到目前为止这样的代码:`[Quotes objectAtIndex:quoteNumber]`不返回任何东西

-(void) CreateArray { 
    Quotes = [NSArray arrayWithObjects: 
       @"Maybe, just once, someone will call me 'Sir' without adding, 'You're making a scene.'", 
       @"You know, boys, a nuclear reactor is a lot like a woman. You just have to read the manual and press the right buttons.", 
       @"When will I learn? The answer to life's problems aren't at the bottom of a bottle, they're on TV!", 
       @"Son, when you participate in sporting events, it's not whether you win or lose: it's how drunk you get.", 
       @"Please don't eat me! I have a wife and kids. Eat them!", 
       @"Marriage is like a coffin and each kid is another nail.", 
       @"Kids, you tried your best and you failed miserably. The lesson is, never try.", 
       @"When I look at the smiles on all the children's faces, I just know they're about to jab me with something.", 
       @"I want to share something with you: The three little sentences that will get you through life. Number 1: Cover for me. Number 2: Oh, good idea, Boss! Number 3: It was like that when I got here.", 
       @"Oh, people can come up with statistics to prove anything, Kent. 14% of people know that.", 
       @"Remember that postcard Grandpa sent us from Florida of that Alligator biting that woman's bottom? That's right, we all thought it was hilarious. But, it turns out we were wrong. That alligator was sexually harrassing that woman.", 
       @"Kill my boss? Do I dare live out the American dream?", 
       @"If something goes wrong at the plant, blame the guy who can't speak English.", 
       @"Alcohol is a way of life, alcohol is my way of life, and I aim to keep it."]; 
} 

-(void) nextQuote:(NSTimer *)timer { 
    quoteNumber++; 
    QuoteLabel.text = [Quotes objectAtIndex: quoteNumber]; 
} 

-(IBAction)nextButton:(id)sender { 
    quoteNumber++; 
    NSString *Quote = [Quotes objectAtIndex: quoteNumber]; 
    QuoteLabel.text = Quote; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(nextQuote:) userInfo:nil repeats:YES]; 
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 
} 

我真的不能看到这段代码的任何问题,但到目前为止,我已经有一个相当大的问题:每当报价应该变化,标签只是空白。我试过使用NSLog来查看[Quotes objectAtIndex: quoteNumber]返回的内容,但它没有记录任何内容。我也尝试使用Quotes[quoteNumber],但它并没有什么不同。

我是Objective-C和iOS开发人员的初学者,所以这可能有一个非常明显的解决方案,但我试图寻找它的网络,找不到任何帮助我的东西。

+1

显示如何声明'行情'。使用调试器。在'nextButton:'和'nextQuote:'中设置一个断点。他们叫吗?变量是否具有您期望的值?并使用标准的命名约定。变量和方法应该以小写字母开头。类应该以大写字母开头。 – rmaddy

回答

2

问题是,你从来没有打电话给CreateArray,所以Quotes数组总是空的。

很少有其他值得注意的事情。类名应该以大写字母开头,方法/属性应该是小写字母。

这些方法

另外:

-(void) nextQuote:(NSTimer *)timer { 
    quoteNumber++; 
    QuoteLabel.text = [Quotes objectAtIndex: quoteNumber]; 
} 

-(IBAction)nextButton:(id)sender { 
    quoteNumber++; 
    NSString *Quote = [Quotes objectAtIndex: quoteNumber]; 
    QuoteLabel.text = Quote; 
} 

你会最终得到索引越界,因为你永远重置quoteNumber。每当quoteNumber和物体在你的Quotes阵列的数量是相同的像

if(quoteNumber % self.Quotes.count == 0){ 
    quoteNumber = 0; 
} 

会重置。

最后,什么是Quotes?这是一个财产吗?如果是这样你应该选择像重写吸气

- (NSArray *)quotes 
{ 
    if(!_quotes){ 
    _quotes = @[]; //Here is where you'd make your array with objects 
} 

这应该让你和运行。

+0

谢谢你,让索引超出范围,我只是用这个表达式来确保问题不在我之前的表达式上。我用'NSArray * Quotes'在.h文件中创建了引号,我知道这不是最好的方式,是吗? – AugustoQ

+0

没有什么可以工作,但是使用'quotes'而不是'Quotes'。 –