iphone - 显示 Null 输出的按钮

标签 iphone objective-c ios

问题ViewController.h:

#import <UIKit/UIKit.h>
@interface QuestionsViewController : UIViewController
{    
    int currentQuestionIndex; 
    NSMutableArray *questions; 
    IBOutlet UILabel *questionField; 
}

-(IBAction)showQuestion:(id)sender;
@end

问题ViewController.m:
#import "QuestionsViewController.h"

@implementation QuestionsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"First", @"First");
    self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}

- (id)init
{
// Call the init method implemented by the superclass
self = [super init];
if (self) {
// Create two new arrays and make the pointers point to them
 questions = [[NSMutableArray alloc] init];

    //Add questions and answers to the array
    [questions addObject:@"Who are you?"];

    [questions addObject:@"Are your talking to me?"];

}
// Return the address of the new object
return self;
}

- (IBAction)showQuestion:(id)sender
{
// Step to the next question
currentQuestionIndex++;

// Am I past the last question?
if (currentQuestionIndex == [questions count]) {
    // Go back to the first question
    currentQuestionIndex = 0;
}

// Gets the string at the index in the questions array
NSString *question = [questions objectAtIndex:currentQuestionIndex];

// Log the string to the console
NSLog(@"displaying question: %@", question);

// Display the string in the question field
[questionField setText:question];
}

// Removed ViewLifeCycle Code

@end

这在模拟器中构建并运行,但是当我按下 showQuestion 按钮时,我得到以下输出:
2012-01-12 11:15:59.180 2Rounds3[1036:f803] displaying question: (null)

我究竟做错了什么?

最佳答案

我的猜测是你永远不会调用 -init方法。

我想您正在使用 -initWithNibName:bundle 创建 View Controller 方法。我对么?如果是这样,你是 -init方法永远不会被调用。所以不要设置实例变量 questions-init方法在您的主要初始化方法中执行:-initWithNibName:bundle .

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

        // Create two new arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];

        //Add questions and answers to the array
        [questions addObject:@"Who are you?"];

        [questions addObject:@"Are your talking to me?"];
    }
    return self;
}

关于iphone - 显示 Null 输出的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8838590/

相关文章:

ios - IBOutlets强弱

第一次在 objective-c 中创建程序循环

ios - 如何在 UITableViewCell 中使用 UIAlertController

android - 适用于 Android/iphone/ipad 的开源或其他 pgp/gpg 库

ios - 从 swift 中的类中获取值 -> 调用中参数 "userName"缺少参数

iphone - 没有角标(Badge)/警报/声音的 APNS

iphone - 如何在 UISearchBar 变为事件状态时展开它

ios - 尝试将非属性列表对象设置为 NSUserDefaults

ios - 如何在运行时确定只读属性是否弱?

ios - 为什么当我删除 TableView 中的一行时它会删除多行?