Objective c objective C OSX-委托-从另一个类调用子类(不是作为实例)
我正在尝试一个代理,但它不起作用。 changeView\u ShowContact方法将使视图控制器显示良好。但是,当我从另一个类调用changeView\u ShowContact时,它将不起作用Objective c objective C OSX-委托-从另一个类调用子类(不是作为实例),objective-c,Objective C,我正在尝试一个代理,但它不起作用。 changeView\u ShowContact方法将使视图控制器显示良好。但是,当我从另一个类调用changeView\u ShowContact时,它将不起作用 @protocol callingActions_fromMainNavigation <NSObject> - (IBAction)changeView_ShowContact:(id)sender; @end **@interface Navigation_Main :
@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.
}
主导航中的协议调用操作
-(iAction)changeView\u ShowContact:(id)发件人;
@结束
**@界面导航\u主:NSViewController**
@属性(非原子,强)id委托;
**@实现导航_Main**
@综合代表;
-(iAction)changeView\u ShowContact:(id)发件人;
{
NSLog(@“************************显示联系人”);
AddStuffViewController=[[pageContact alloc]initWithNibName:@“pageContact”捆绑包:nil];
[AddStuffView addSubview:[AddStuffViewController view]];//当使用类的非共享实例(不是单例的对象)时,为什么希望一个类的新初始化实例共享对任何其他引用的引用?那将是一场灾难!您需要获得对该视图控制器的有效引用(理想情况下)拥有
contacts
对象,而不是尝试初始化自己的对象,并祈祷出现重大malloc()错误。我已修复了您的代码(包括某些类的名称):
//为了安全起见,即使是代理也需要标准的“名称空间”前缀
@协议C接触
-(iAction)changeView\u ShowContact:(id)发件人;
@结束
//对于一个类来说,这是一个糟糕的名字。类别是唯一的类
//这应该使用下划线中间名,甚至只有在编译时才使用下划线
@接口CFIMainNavigationViewController:NSViewController
//代表**永远**坚强
@属性(非原子,弱)id委托;
@CFIMainNavigationViewController的实现
//@不必要的;不必要的
-(iAction)changeView\u ShowContact:(id)发件人;
{
NSLog(@“************************显示联系人”);
AddStuffViewController=[[pageContact alloc]initWithNibName:@“pageContact”捆绑包:nil];
[AddStuffView addSubview:[AddStuffViewController视图]]//
@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;
{
}
//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