나는 몇 가지 버튼과 스토리 보드를 사용하여 라벨을 만들었습니다. XCode 4.6.3
FirstViewController에 배치 된 버튼을 클릭하고 몇 가지 메서드를 실행하려면 몇 가지 메서드를 실행하고 싶습니다. 일부 변수/NSMutableArrays
(currentQuestionIndex, questions 등)을 정의했습니다.). 나는 inititalizing을 위해 커스텀 init 메소드를 사용하고자한다. 그것이 그 안에 내 구현을 새로운 초기화 방법을 작성하고 쓰기로 나는 initWithNibName
및 initWithCoder
을 떠날 때 , 그것은 호출되지 않습니다. 나는 아래의 미리보기에 표시된대로 내가 코드를 조작 할 때스토리 보드를 사용할 때 objective-c로 커스텀 init 메소드를 구현하는 방법은 무엇입니까?
는하지만, 그것은했다.
나는 내가 여기했던 것은 아마 좋은 방법이 아닙니다 때문에 Storyboard
를 사용하여 객체를 생성 할 때 사용자 정의 초기화하기 방법을 사용하는 방법을 알고 싶어요. initWithCoder
을 사용하여 초기화를 시도했지만 작동하지 않았습니다. 하지만 내가 기억할 수있는 것에서는 을 사용하여 Storyboard
에서 초기화 할 수 있습니다. 맞습니까? storyboard
의 버튼/레이블이 FirstViewController에 있습니다.
이 내 FirstViewController.h 파일입니다
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
{
int currentQuestionIndex;
// The model objects
NSMutableArray *questions;
NSMutableArray *answers;
//The view objects
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
}
@property (strong, nonatomic) UIWindow *window;
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
@end
및
이 내 FirstViewController.m 파일
#import "FirstViewController.h"
@interface FirstViewController()
@end
@implementation FirstViewController
- (id)init {
// Call the init method implemented by the superclass
self = [super init];
if(self) {
currentQuestionIndex = -1;
// Create two arrays and make the pointers point to them
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// Add questions and answers to the arrays
[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 the address of the new object
return self;
}
-(IBAction)showQuestion:(id)sender
{
currentQuestionIndex++;
if(currentQuestionIndex == [questions count])
currentQuestionIndex = 0;
NSLog(@"%d",[questions count]);
NSString *question = [questions objectAtIndex:currentQuestionIndex];
NSLog(@"dislaying question at index %d : %@" ,currentQuestionIndex,question);
[questionField setText:question];
[answerField setText:@"???"];
}
-(IBAction)showAnswer:(id)sender
{
NSString *answer = [answers objectAtIndex:currentQuestionIndex];
[answerField setText:answer];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
예에서 initWithCoder (및 init이 아닌)를 재정의해야합니다. "효과가 없다"는 것은 무엇을 의미합니까? 호출되지 않았습니까? - 모든 객체가 스토리 보드에서로드되고 콘센트가 연결될 때 호출되는 awakeFromNib를 재정의 할 수도 있습니다. –
initWithCoder를 재정의하려 할 때 지정한 초기화가 실패했습니다. 그리고 awakeFromNib과 정확히 무슨 상관이 있습니까? – ronilp
"작동하지 않았다"는 것은 무엇을 의미합니까? –