2013-03-09 62 views
0

我试着委托,但它不工作。 changeView_ShowContact方法将使视图控制器显示得很好。但是当我从另一个类调用changeView_ShowContact时,它不会工作。目标C OSX - 委托 - 从另一个类别调用子(不作为实例)

@protocol callingActions_fromMainNavigation <NSObject> 
    - (IBAction)changeView_ShowContact:(id)sender; 
@end 

**@interface Navigation_Main : NSViewController** 
@property (nonatomic, strong) id <callingActions_fromMainNavigation> delegate; 

**@implementation Navigation_Main** 
@synthesize delegate; 

- (IBAction)changeView_ShowContact:(id)sender; 
{ 
    NSLog(@"********************ShowContact"); 
    AddStuffViewController = [[pageContact alloc] initWithNibName:@"pageContact" bundle:nil]; 
    [AddStuffView addSubview:[AddStuffViewController view]]; //<This call here works ok. 
} 

@interface contacts : NSObject <callingActions_fromMainNavigation> 

**@implementation contacts** 
-(void)myMethodCall:(id)sender; 
{ 
    Navigation_Main *NavMain = [[Navigation_Main alloc] initWithNibName:@"Navigation_Main.h" bundle:nil]; 
    [NavMain setDelegate:self]; 
    [self changeView_ShowContact:nil]; 
//I need to call the (IBAction)changeView_ShowContact in the Main Navigation. This 
//code is not working. 
} 

- (IBAction)changeView_ShowContact:(id)sender; 
{ 
} 

回答

0

当使用非共享的实例类的(东西是不是单身),为什么你指望类的新实例初始化分享给其他任何参考参考?那将是一场灾难!你需要获得对视图控制器的有效引用(理想情况下)拥有该对象,而不是尝试并初始化你自己并为主要的malloc()错误祈祷。我已修复您的代码(包括某些类的名称):

//Even delegates need a standard "namespace" prefix for safety 
@protocol CFIContactsNavigation<NSObject> 
    - (IBAction)changeView_ShowContact:(id)sender; 
@end 

// This is a terrible name for a class. Categories are the only classes 
// that should use underscores mid-name, and even then only when compiled 
@interface CFIMainNavigationViewController : NSViewController 
//delegates are **never** strong 
@property (nonatomic, weak) id <CFIContactsNavigation> delegate; 

@implementation CFIMainNavigationViewController 
//@synthesize delegate; unnecessary 

- (IBAction)changeView_ShowContact:(id)sender; 
{ 
    NSLog(@"********************ShowContact"); 
    AddStuffViewController = [[pageContact alloc] initWithNibName:@"pageContact" bundle:nil]; 
    [AddStuffView addSubview:[AddStuffViewController view]]; //<This call here works ok. 

    CFIContacts *contacts = [CFIContacts new]; 
    [self setDelegate:contacts]; 
    [contacts setNavMain:self]; 
} 

//Again, terrible name for a class. Also, it's missing an @end 
@interface CFIContacts : NSObject <CFIContactsNavigation> 

@property (nonatomic, weak) CFIMainNavigationViewController *navMain; 

@end 

@implementation CFIContacts 
-(void)myMethodCall:(id)sender; 
{ 
    //instance names are pascal-cased 
    // CFIMainNavigationViewController *navMain = [[Navigation_Main alloc] initWithNibName:@"Navigation_Main.h" bundle:nil]; 
    [self.mainNav changeView_ShowContact:nil]; 
//I need to call the (IBAction)changeView_ShowContact in the Main Navigation. This 
//code is not working. 
} 

//Why the heck are there a pair of parenthesis *and* a semicolon? 
// this would never compile 
- (IBAction)changeView_ShowContact:(id)sender { } 

//... 

@end