0

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

视图控制器.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

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

问题是当我点击按钮时,什么也没有发生。知道我错过了什么吗?

提前致谢!

4

1 回答 1

0

您在 viewDidLoad 上将报价文本设置为零。如果您摆脱它,只要您将按钮正确绑定到 viewcontroller 函数,您的代码就应该可以工作

quote_text = nil;
于 2012-06-02T15:09:20.943 回答