如何在可可中的普通void函数中调用 - (void)函数

时间:2016-09-21 06:27:48

标签: objective-c cocoa

例如:

-(void) myExample {  
    ..do something   
}  

void myOther(){  
    how to call myExample function here  
} 

3 个答案:

答案 0 :(得分:0)

如果两个方法都在同一个类中,则可以直接从第二个方法调用First方法,如下所示:

-(void) myExample {  
    ..do something   
}  

void myOther(){  
   call to myExample function
   [self myExample];
} 

在此处阅读文档:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

答案 1 :(得分:0)

void getInputSource() {  
    TISInputSourceRef source = TISCopyCurrentKeyboardLayoutInputSource();  
    NSLog(@"languages: %@", TISGetInputSourceProperty(source, kTISPropertyBundleID));  
    NSLog(@"localized name: %@", TISGetInputSourceProperty(source, kTISPropertyLocalizedName));
    [self awakeFromNib];  
}  

-(void) awakeFromNib {  
    self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];  
    NSImage* icon = [NSImage imageNamed:@"icon.png"];  
    self.statusBar.image = icon;  
}

答案 2 :(得分:0)

当您致电myOther时,请传递自我参考。你应该像这样定义C方法:

void myOther(id callBack)

现在你在c函数中有自引用。

void myOther(id callBack){  
   [callBack myExample];
}