单击按钮时无动作;报价不会改变

时间:2012-06-02 15:02:09

标签: objective-c cocoa

我已经整理了一个简单的引用生成器,将引号存储在一个数组中。以下是引用视图控制器接口和实现文件:

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将文本视图和按钮连接到文件所有者。

麻烦的是,当我点击按钮时,没有任何反应。知道我错过了什么吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

在viewDidLoad上将您的设置引用文本设置为nil。如果你摆脱了你的代码应该工作,只要你正确地将按钮绑定到viewcontroller函数

quote_text = nil;