2012-12-02 28 views
5

में प्रोग्रामेमिक रूप से एक्शन श्रोता प्रोग्राम सेट करें हाय मैंने प्रोग्रामेटिक रूप से एक बटन बनाया है। मैं नेविगेशन बार में यह बटन जोड़ दूंगा। अब मैं इसे एक टच अप अंदर कार्रवाई श्रोता जोड़ना चाहता हूं। मैं यह कैसे करुं? धन्यवाद।आईओएस

उत्तर

6

चूंकि आपने उन्हें नेविगेशन बार में जोड़ा है, यह थोड़ा अलग है, लेकिन मूल रूप से वही है। जब आप बटन बनाते हैं तो आप श्रोता/हैंडलर जोड़ देंगे। निम्नलिखित यहाँ मैं का उपयोग कर एक नेविगेशन पट्टी करने के लिए << और >> जोड़ दिया है:

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@">>" style:UIBarButtonItemStylePlain target:self action:@selector(navNextButtonPressed)]; 
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(navPrevButtonPressed)]; 
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:nextButton, prevButton, nil]; 

और उसके बाद सामान्य तौर पर अपने हैंडलर बनाने के लिए:

#pragma mark - button handling 
-(void)navNextButtonPressed 
{  
    NSLog(@"Next pressed"); 
} 

-(void)navPrevButtonPressed 
{ 
    NSLog(@"Prev pressed"); 
} 
+0

अच्छा स्पष्टीकरण धन्यवाद! – Tahlil

16

यूआईबटन यूआईसींट्रोल का उप-वर्ग है।

बटन बनाने के बाद आपको बस बटन की लक्ष्य और कार्रवाई सेट करने के लिए करना होगा। यानी

// Create your button: 
UIButton *button = // However you create your button 

// Set the target, action and event for the button 
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain. 
      action:// A selector for the method 
forControlEvents:UIControlEventTouchUpInside]; 
+2

यह iOS..So के लिए है यह UIControl का एक उपवर्ग है? ? सही? –