2013-12-14 10 views
2

나는 몇 가지 버튼과 스토리 보드를 사용하여 라벨을 만들었습니다. XCode 4.6.3 FirstViewController에 배치 된 버튼을 클릭하고 몇 가지 메서드를 실행하려면 몇 가지 메서드를 실행하고 싶습니다. 일부 변수/NSMutableArrays (currentQuestionIndex, questions 등)을 정의했습니다.). 나는 inititalizing을 위해 커스텀 init 메소드를 사용하고자한다. 그것이 그 안에 내 구현을 새로운 초기화 방법을 작성하고 쓰기로 나는 initWithNibNameinitWithCoder을 떠날 때 , 그것은 호출되지 않습니다. 나는 아래의 미리보기에 표시된대로 내가 코드를 조작 할 때스토리 보드를 사용할 때 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 
+2

예에서 initWithCoder (및 init이 아닌)를 재정의해야합니다. "효과가 없다"는 것은 무엇을 의미합니까? 호출되지 않았습니까? - 모든 객체가 스토리 보드에서로드되고 콘센트가 연결될 때 호출되는 awakeFromNib를 재정의 할 수도 있습니다. –

+0

initWithCoder를 재정의하려 할 때 지정한 초기화가 실패했습니다. 그리고 awakeFromNib과 정확히 무슨 상관이 있습니까? – ronilp

+0

"작동하지 않았다"는 것은 무엇을 의미합니까? –

답변

2

내가 로그인하는 모든 values..when를 받고 엄마입니다 .. . 시도해보십시오.

-(id)initWithCoder:(NSCoder *)aDecoder{ 

    if(self = [super initWithCoder:aDecoder]) { 

     currentQuestionIndex = 15; 
     NSLog(@"%d", currentQuestionIndex); 
     // 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"]; 
     NSLog(@"Questions = %@", questions); 
     NSLog(@"Answers = %@", answers); 
    } 
    return self; 
} 
+0

좋아요, 제 문제가 있습니다. 내가 잘못하고있는 것은 self = [super initWithCoder] 대신에 self = [super init]을 사용했다는 것입니다. 그것으로 해결됩니다. 당신이 설명한다면 난 그냥 NSObject의 (UINibLoadingAdditions를) 발견 – ronilp

0

UIViewController

+0

이 더 나은 해답이 될 것입니다. 나는 5 분 안에 내 downvote를 되 찾을거야. –

+1

작동 왜 감사 –