0

我正在尝试委托,但它不起作用。changeView_ShowContact 方法将使视图控制器正常显示。但是当我从另一个类调用 changeView_ShowContact 时,它不会工作。

@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;
{
}
4

1 回答 1

0

当使用一个类的非共享实例(不是单例的东西)时,为什么你会期望一个新的类初始化实例共享对任何其他引用的引用?那将是一场灾难!您需要获得对(理想情况下)拥有该contacts对象的视图控制器的有效引用,而不是尝试初始化您自己的并祈祷重大 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
于 2013-03-10T07:57:01.107 回答