2011-09-09 15 views
7

काम नहीं कर रहा है मैं प्रोग्रामिंग के कुछ बटन बना रहा हूं और प्रत्येक को एक क्रिया सेट करने की कोशिश कर रहा हूं लेकिन मुझे इसे काम करने के लिए प्रतीत नहीं होता है।उद्देश्य-सी: एनएसबटन सेटएक्शन

मेरी AppController.h में मैं इस कोड है:

... 
IBOutlet NSButton* btnZoomIn; 
IBOutlet NSButton* btnZoomOut; 
... 

और

- (IBAction) zoomIn : (id) sender; 

- (IBAction) zoomOut : (id) sender; 

और AppController.m में awakeFromNib विधि के भीतर:

/*zoom in and out buttons*/ 

//get the path to the image files 
NSString* zoomInImgPath = [[NSBundle mainBundle] pathForResource:@"zoomIn" ofType:@"png"]; 
NSString* zoomOutImgPath = [[NSBundle mainBundle] pathForResource:@"zoomOut" ofType:@"png"]; 

//declare the NSImages 
zoomInImg = [[NSImage alloc] initWithContentsOfFile:zoomInImgPath]; 
zoomOutImg = [[NSImage alloc] initWithContentsOfFile: zoomOutImgPath]; 

//button making! 
//zoomIn 
btnZoomIn = [[NSButton alloc] initWithFrame:NSMakeRect(1426.0, 920.0, 25.0, 25.0)]; 
[btnZoomIn setButtonType:NSMomentaryPushInButton]; 
[btnZoomIn setTitle:@""]; 
[btnZoomIn setToolTip:@"Zoom In"]; 
[btnZoomIn setImage:zoomInImg]; 
[btnZoomIn setAction:@selector(zoomIn:)]; 
[[mainWin contentView] addSubview:btnZoomIn]; 

//zoomOut 
btnZoomOut = [[NSButton alloc] initWithFrame:NSMakeRect(1456.0, 920.0, 25.0, 25.0)]; 
[btnZoomOut setButtonType:NSMomentaryPushInButton]; 
[btnZoomOut setTitle:@""]; 
[btnZoomOut setToolTip:@"Zoom Out"]; 
[btnZoomOut setImage:zoomOutImg]; 
[btnZoomOut setAction:@selector(zoomOut:)]; 
[[mainWin contentView] addSubview:btnZoomOut]; 

और

- (IBAction) zoomIn : (id) sender { 
    NSLog(@"zoom in!"); 
} 

- (IBAction) zoomOut : (id) sender { 
    NSLog(@"zoom out!"); 
} 

लेकिन Zoomout और zoomin मारा नहीं मिलता ...

+4

रूप में अच्छी तरह 'target' सेट करने का प्रयास भूल गया था। – PengOne

+0

यदि आप एआरसी या जीसी का उपयोग नहीं कर रहे हैं, तो छवियों और बटनों को रिलीज़ या ऑटोरेज़ करना न भूलें। –

उत्तर

28

की सर्वाधिक संभावना उन कार्यों का जवाब responder chain for action messages में कोई वस्तु नहीं है कि है। जब आप कोई क्रिया निर्दिष्ट करते हैं लेकिन लक्ष्य निर्दिष्ट नहीं करते हैं, तो कोको उस ऑब्जेक्ट को खोजने का प्रयास करता है जो प्रतिक्रियाकर्ता श्रृंखला को घुमाने के द्वारा उन क्रिया संदेशों का जवाब देता है।

[btnZoomIn setAction:@selector(zoomIn:)]; 
[btnZoomIn setTarget:objectThatRespondsToZoomIn]; 

[btnZoomOut setAction:@selector(zoomOut:)]; 
[btnZoomOut setTarget:objectThatRespondsToZoomOut]; 
0

मैं समझ नहीं सका:

या तो सुनिश्चित करें कि आप प्रत्युत्तर श्रृंखला में एक उपयुक्त वस्तु है कि या यदि आप उस वस्तु के लिए एक संदर्भ है, उन कार्यों का लक्ष्य के रूप में यह निर्दिष्ट करते हैं, कर क्यों मेरा setActionsetTarget के साथ भी काम नहीं कर रहा था।

मुद्दा मैं NSButton subclassed और onMouseDown overrode और कॉल करने के लिए super.onMouseDown