2012-05-03 27 views
6

Tôi muốn trình điều khiển chế độ xem có thể truy cập dog trong ủy nhiệm ứng dụng.Làm thế nào để truy cập các biến điều khiển xem từ ứng dụng đại biểu ... và ngược lại?

Tôi muốn ứng dụng ủy quyền có thể truy cập mouse trong trình điều khiển chế độ xem.


#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    int mouse; // <---------------- 
} 
@end 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    mouse = 12; // <------------------- 

    NSLog(@"viewDidLoad %d", dog); // <--------------- 
} 

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    int dog; // <--------------- 
} 
@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@end 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    NSLog(@"applicationWillResignActive %d", mouse); // <-------------- 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    dog = 77; // <--------------------- 

    NSLog(@"applicationDidBecomeActive"); 
} 
+0

Làm cách nào để hai đối tượng truy cập vào các biến của nhau? –

Trả lời

9

Phần 1: Trong ViewController.h:

-(int)mouse; //add this before the @end 

Trong ViewController.m, thêm phương pháp này:

-(int)mouse 
{ 
    return mouse; 
} 

Để truy cập chuột từ appdelegate, sử dụng self.viewController.mouse Đối thí dụ;

NSLog(@"ViewController mouse: %i", self.viewController.mouse); 

Part2:

Trong AppDelegate.h:

-(int)dog; //add this before the @end 

Trong AppDelegate.m, thêm phương pháp này:

-(int)dog 
{ 
    return dog; 
} 

Trong ViewController.m:

#import "AppDelegate.h" 

Để truy cập chó từ ViewController, sử dụng này:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSLog(@"dog from AppDelegate: %i", [appDelegate dog]); //etc. 
+0

Trong Phần 1: 'self.viewController.mouse' có thể gửi đối số không? – YumYumYum

6

Theo quan điểm của tập tin header điều khiển của bạn thêm chuột như một thuộc tính:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    NSInteger mouse; // <---------------- 
} 

@property (nonatomic, assign) NSInteger mouse; 

@end 

Tổng hợp tài sản theo quan điểm thực hiện điều khiển của bạn ngay dưới @ dòng triển khai:

@synthesize mouse; 

Trong ứng dụng, bạn thêm chó làm tài sản:

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    NSInteger dog; // <--------------- 
} 
@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@property (nonatomic, assign) NSInteger dog; 

@end 

Cũng tổng hợp chó trong việc triển khai ủy nhiệm ứng dụng của bạn.

Bây giờ trong đại biểu ứng dụng của bạn, giả sử bạn có một tham chiếu đến điều khiển xem, bạn có thể truy cập con chuột như thế này:

viewController.mouse = 13; 

Bạn có thể làm điều tương tự với lớp ứng dụng đại biểu của mình, mà có thể được truy cập từ bất kỳ bộ điều khiển chế độ xem nào bằng cách sử dụng (giả sử tên của lớp người dùng ứng dụng của bạn là AppDelegate):

((AppDelegate *)([UIApplication sharedApplication].delegate)).dog = 13; 

Tôi khuyên bạn nên sử dụng NSInteger thay vì int.

+0

(viewController.mouse = 13;) cần "self" -> (self.viewController.mouse = 13;) – jdl

+0

Điều đó thực sự sẽ phụ thuộc vào việc viewController có hay không là một thuộc tính tổng hợp của ủy nhiệm ứng dụng. Nếu đó là sau đó có điều này sẽ là phương pháp ưa thích của việc truy cập nó. –

Các vấn đề liên quan