2012-01-03 3 views
0

보기 컨트롤러 클래스의 SecondViewController에서 단추가있는 배열을 삭제하는 데 도움이 필요합니다. 순간 C-Objective의 다른 클래스에서 NSMutableArray를 삭제 하시겠습니까?

나는이 있습니다

- (IBAction)deleteArrayFromSeconViewController:(id)sender // is the Button which should       
                  // delete the array 
{ 
    self.textLabel2.text = @"";       // this work fine 
    ViewController *vc = [[ViewController alloc]init]; 
    [vc.textViewArray removeAllObjects];     // do not remove the objects? 

} 

나는 ViewControllerClass에서 SecondViewControllerClass에서 주문을 추월하기 위해 어떻게해야합니까?

나는 SecondViewControllerClass 또한이 시도 :

- (void) deleteTheArray 
{ 
    [textViewArray removeAllObjects]; 
} 
+5

새 "컨트롤러를 만드는 것보다 현재"다른보기 컨트롤러 "가 필요합니다. – Krizz

+1

배열을 지우기 위해 호출하는 다른 ViewController에서 메소드를 작성하여 데이터를 그 아래에서 꺼내는 대신 모든 가사 코드를 실행하도록 선택할 수 있습니다. 필요한 정리 작업이 없어도 향후 리팩터링을 쉽게 할 수 있습니다. :) – matthias

답변

1

나는 모든이이 작업을 수행하는 가장 좋은 방법입니다 모르겠어요 :

- (IBAction)deleteArrayFromSeconViewController:(id)sender 
{ 
    self.textLabel2.text = @""; 

    ViewController *vc = [[ViewController alloc]init]; 
    [vc deleteTheArray]; 

} 

가 ViewControllerClass에서이 함수를 호출 하지만 첫 번째보기 컨트롤러에 사용자 정의 NSNotification을 게시 한 다음 두 번째보기 컨트롤러에서 가져올 수 있습니다. 코드 예 :

//Post in your first view controller when wanting to delete the array 
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteArrayNotification" object:nil userInfo:nil /*include any items your reciever might wish to access*/]; 

다음 두 번째 뷰 컨트롤러에, 당신은 -(void)awakeFromNib 방법의 관찰자로서 자신을 추가 할 수 있습니다. 펜촉 방법에서 깨어에 넣어 :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteArrayNotification:) name:@"DeleteArrayNotification" object:nil]; 

그리고 다음 deleteArrayNotification 구현 : 방법 :

- (void)deleteArrayNotification:(NSNotification *)notification { 
[array removeAllObjects]; 
[array release]; //Delete this line if your project is using ARC 
} 

I는이 같은 NSNotification을 구현하는 좋은 코딩 연습 가능성이 매우 의심 오전,하지만 난 그것을 생각 사용 중일 수도 있습니다!

자세한 내용은 Apple 개발자 설명서 NSNotificationNSNotificationCenter 아래에 있습니다.

희망을 도울 수 있습니다.

관련 문제