2012-06-02 3 views
0

Я собрал простой генератор цитат, сохранив кавычки в массиве. Ниже приведен интерфейс и реализация котировки вида контроллер файлы:Никаких действий при нажатии кнопки; цитата не изменяется

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@property(nonatomic, retain)NSArray *myQuotes; 
@property(nonatomic, retain)NSMutableArray *movieQuotes; 
@property (nonatomic, retain) IBOutlet UITextView *quote_text; 

-(IBAction)quote_btn_touch:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize myQuotes; 
@synthesize movieQuotes; 
@synthesize quote_text; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.myQuotes = [NSArray arrayWithObjects: 
        @"Live and let live", 
        @"Don't cry over spilt milk", 
        @"Always look on the bright side of life", 
        @"Nobody's perfect", 
        @"Can't see the woods for the trees", 
        @"Better to have loved and lost than not loved at all", 
        @"The early bird catches the worm", 
        @"As slow as a wet week", 
        nil]; 

    quote_text = nil; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

-(IBAction)quote_btn_touch:(id)sender { 
    // 1 - Get number of rows in array 
    int array_tot = [self.myQuotes count]; 
    // 2 - Get random index 
    int index = (arc4random() % array_tot); 
    // 3 - Get the quote string for the index 
    NSString *my_quote = [self.myQuotes objectAtIndex:index]; 
    // 4 - Display the quote in the text view 
    self.quote_text.text = [NSString stringWithFormat:@"Quote:\n\n%@", my_quote];  
} 

@end 

В файле XIb я подключил вид текста и кнопку для данного файла Владелец, используя quote_text и quote_btn_touch соответственно.

Проблема в том, что когда я нажимаю на кнопку, ничего не происходит. Любая идея, что я пропустил?

Заранее благодарен!

ответ

0

ваш текст ссылки установки в ноль на viewDidLoad. Если вы избавитесь от этого, ваш код должен работать до тех пор, пока вы правильно связали кнопку с функцией viewcontroller.

quote_text = nil; 
+0

Спасибо. Я вижу, что я должен был поместить это под метод viewDidUnload вместо viewDidLoad. Еще раз спасибо! – pdenlinger

Смежные вопросы