0

デリゲートを試していますが、機能していません。changeView_ShowContact メソッドは、View Controller を正常に表示するようにします。しかし、別のクラスから 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そのオブジェクトを (理想的には) 所有するビュー コントローラーへの有効な参照を取得する必要があります。コードを修正しました (一部のクラスの名前を含む):

//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 に答える