2011-09-22 5 views
1

버튼 클릭시 텍스트가있는 UILabel 텍스트를 변경하려고하지만 아무 것도하지 않습니다.NSMutablearray 데이터로 UILabel의 텍스트 변경

@interface Test01AppDelegate : NSObject <UIApplicationDelegate> { 
UILabel *helloLabel; 
UIButton *hellobutton; 
NSMutableArray *madWords; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UIButton *hellowButton; 
@property (nonatomic, retain) IBOutlet UILabel *hellowLabel; 

@property (nonatomic, retain) NSMutableArray *madWords; 

- (void) madArrays; 
- (IBAction)helloYall; 

@end 

#import "Test01AppDelegate.h" 

@implementation Test01AppDelegate 

@synthesize window = _window; 
@synthesize hellowButton; 
@synthesize hellowLabel; 
@synthesize madWords; 

- (void) madArrays { 
[madWords addObject:@"Part A"]; 
[madWords addObject:@"Part B"]; 
[madWords addObject:@"Part C"]; 
[madWords addObject:@"Part D"]; 
} 

- (IBAction)helloYall { 

[self madArrays]; 

self.hellowLabel.text = [madWords objectAtIndex:0]; 

} 

나는

@"some text here"; 

으로 helloLabel 텍스트를 설정하고 그것을 잘 작동합니다. 또한 "madArrays"메서드를 "helloYall"메서드로 복사하려고 시도했지만 여전히 작동하지 않았습니다. 앞에서 말했듯이 필자는 텍스트를 수동으로 설정할 수 있으며 작동하지만 배열에서 정보를 가져오고 싶습니다. 결국, 배열을 반복하여 각 버튼 누름에서 텍스트를 가져오고 싶지만 한 번에 한 단계 씩 진행하고 싶습니다. 감사.

답변

3

madWords 배열을 만들지 마십시오.

self.madWords = [NSMutableArray array]; 

를 상단에 : 당신은 추가해야

- (void) madArrays { 

아마 좋은 장소가 될 것입니다. 다른 좋은 곳은 클래스 init이거나 뷰 컨트롤러 viewWillAppear 메서드입니다. 당신이 그것을 채울 올 때 그것은 madArrays처럼 보이는

+0

답변 주셔서 감사합니다. 나는 메시지의 오른쪽 부분에있는 "배열"이 무엇을 의미하는지 또는 어떤 의미인지에 대해서 조금 혼란스러워한다. – anthony

+0

'[NSMutableArray array]'는 자동으로 릴리즈 된 NSMutableArray를 생성합니다. 'array'는 클래스 메쏘드 이름입니다. – zaph

0
// Or you can try this in your init Method: 

    //first allocate the ivar 

- (void)myInitMethod { 

    madArrays = [[NSMutableArray]alloc]init]; 
} 


//then you can do anything directly to the ivar or throughout de setter 

- (void)doAnythingWithiVar { 

// do your stuff 

} 

    //when you are done you can dealloc your ivar 

- (void)dealloc { 

    [madArrays release]; 
    [super dealloc]; 
} 
0

은 여전히 ​​nil입니다. 어떤 시점에서 [self setMadArrays:[[NSMutableArray alloc] init]];과 같은 것이 필요합니다. 또한 메모리 누수가 있으므로 super으로 전화하기 전에 deallocmadArrays을 출시하는 것을 잊지 마십시오.

관련 문제