Xcode 5.0 を使用して、 Big Nerd Ranch book の第 2 版に従おうとしていますが、これは少し古くなっているようです。
2 つのラベルと 2 つのボタンを持つクイズ プロジェクトの例があります。
私は本からソースコードをコピーしました。AppDelegate.h
:_
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
int currentQuestionIndex;
NSMutableArray *questions;
NSMutableArray *answers;
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
}
@property (strong, nonatomic) UIWindow *window;
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
@end
本にはMainWindow.xib
言及されていませんがMain.storyboard
、ラベルとボタンを配置した があります。
ソース コード エディターの左側 (上のスクリーンショットの中央) と "接続インスペクター" (たとえば、"Touch Up Inside") に 4 つの中空の小さな円が表示されますが、それらを取得できません。接続されました。小さな円からボタン/ラベルにドラッグしてCtrlキーを押しながらドラッグしようとしましたが、青い線がありますが、接続しません。
ボタン/ラベルを右クリックすると、「アウトレット」に関する灰色のメニューが表示されますが、IBOutlets/IBActions がそこにリストされていません。
ラベルとボタンに接続を追加するにはどうすればよいですか?
アップデート:
Rob のアドバイス (ありがとう +1) で、プロパティとメソッドを に移動しViewController.*
、ラベルとボタンを接続できるようになりました。ボタンをクリックすると、メソッドが呼び出されているのがわかります。
しかし今init
、クラスのメソッドがViewController
実行されていないため、両方の配列が nil であるという問題があります。
さらにヒントはありますか?そして、質問を「広すぎる」として閉じる提案がなぜなのかわかりません-(短い)コードとスクリーンショットを添付しました.2つの質問はかなり具体的です(そしておそらく基本的です)。
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
int currentQuestionIndex;
NSMutableArray *questions;
NSMutableArray *answers;
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
}
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (id)init { // XXX is never called??
self = [super init];
if(self) {
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
[questions addObject:@"What is 7 + 7?"];
[answers addObject:@"14"];
[questions addObject:@"What is the capital of Vermont?"];
[answers addObject:@"Montpelier"];
[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];
}
return self;
}
- (IBAction)showQuestion:(id)sender // XXX runs ok when clicked
{
currentQuestionIndex++;
if (currentQuestionIndex == [questions count]) {
currentQuestionIndex = 0;
}
NSString *question = [questions objectAtIndex:currentQuestionIndex];
NSLog(@"displaying question: %@", question);
[questionField setText:question];
[answerField setText:@"???"];
}
- (IBAction)showAnswer:(id)sender // XXX runs ok when clicked
{
NSString *answer = [answers objectAtIndex:currentQuestionIndex];
[answerField setText:answer];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end