9

Quản lý bộ nhớ là một vấn đề rất quan trọng trong iPhone. Vì vậy, tôi hỏi một câu hỏi rất chung chung. Có hai cách để gọi một viewController của một lớp khác.Làm thế nào để đẩy viewcontroller (view controller)?

Cách 1:

AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil] autorelease]; 

[self.navigationController pushViewController:viewController animated:YES]; 

Way 2:

#import "AnotherClassViewController.h" 

    @interface ThisClassViewController : UIViewController{ 

     AnotherClassViewController *myViewController; 

    } 

    @property (nonatomic, retain) AnotherClassViewController *myViewController; 

    @end 

    @implementation ThisClassViewController 

    @synthesize myViewController; 

    - (void) pushAnotherViewController{ 

    if(self.myViewController == nil){ 

    AnotherClassViewController *tempViewController = [[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil]; 

    self.myViewController = tempViewController; 

    [tempViewController release]; 
    } 
    [self.navigationController pushViewController:myViewController animated:YES]; 
    } 

- (void)dealloc{ 
self.myViewController = nil; 
} 
@end 

Vì vậy, câu hỏi rõ ràng là, đó là cách tốt nhất để gọi viewController của lớp khác? Way1 hoặc Way2?

Đề xuất và nhận xét được mời công khai.

Vui lòng nhận xét và bỏ phiếu.

Trả lời

7

Cách 1 đơn giản hơn.

Cách 2 cho phép bộ điều khiển đầu tiên giữ tham chiếu đến bộ điều khiển chế độ xem được đẩy. Nếu bạn cần tham chiếu đó, thì điều này sẽ hữu ích.

Không có câu trả lời rõ ràng ở đây. Nó phụ thuộc vào nhu cầu của bạn. Quy tắc chung, tất nhiên, là làm cho mã đơn giản nhất có thể, nhưng không đơn giản hơn.

19

Hmm ... Để giữ cho mọi thứ đơn giản, tại sao không chỉ:

MyViewController* viewController = [[MyViewController alloc] init]; 

[self.navigationController pushViewController:viewController animated:YES]; 
[viewController release]; 
Các vấn đề liên quan