2010-02-16 9 views
8

मैं उस दृश्य की सामग्री को अन्य दृश्य में जोड़ने के लिए तालिका दृश्य के दाईं ओर प्लस बटन जोड़ना चाहता हूं।उद्देश्य-सी में तालिका दृश्य के दाईं ओर प्लस बटन को कैसे जोड़ना है?

तालिका दृश्य के दाईं ओर प्लस बटन कैसे जोड़ें।

उत्तर

9
  1. आईफोन Interface Guidelines Page पर जाएं।

  2. के तहत "तालिका पंक्तियों और अन्य उपयोगकर्ता इंटरफ़ेस तत्वों में उपयोग के लिए स्टैंडर्ड बटन" ContactAdd बटन कॉपी (मैं इसे ContactAdd.png के रूप में यहाँ बचाने के लिए)। इसे अपनी परियोजना में जोड़ें।

  3. cellForRowAtIndexPath में: (NSIndexPath *) indexPath पद्धति जोड़ें:

    UIImage *image = [UIImage imageNamed:@"ContactAdd.png"]; 
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //You can also Use: 
    //UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    
    //match the button's size with the image size 
    button.frame = frame; 
    
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    
    // set the button's target to this table view controller so you can open the next view 
    [button addTarget:self action:@selector(yourFunctionToNextView:) forControlEvents:UIControlEventTouchUpInside]; 
    
    button.backgroundColor = [UIColor clearColor]; 
    
    cell.accessoryView = button; 
    
+12

* नोट: कॉपी करने और जोड़ने के बजाय, आप इसका भी उपयोग कर सकते हैं: [UIButton बटनविथ टाइप: UIButtonTypeContactAdd]; UIButtonTypeCustom के बजाय। – erastusnjuki

+1

इनमें से किसी एक को करने के साथ मुश्किल हिस्सा आपके फंक्शनक्शन को देखने के लिए पैरामीटर पास कर रहा है: यह जानने के लिए कि कौन सी पंक्ति दबाई गई थी। यह बटन.tag = पंक्ति के साथ आसानी से किया जा सकता है; लेकिन यदि आपके टेबल में अनुभाग हैं तो एक int int पर्याप्त नहीं है। आपको इंडेक्सपाथ पास करना होगा। मुझे यह तरीका ऐसा करने के लिए यहां मिला है। http://stackoverflow.com/questions/5500327/subclass-uibutton-to-add-a-property – roocell

+0

हाँ पैरामीटर पास करना मुश्किल हिस्सा है। आपका लिंक लगभग पूरा हो गया है, सिवाय यह आपको यह नहीं बताता कि यूआईबटन क्लास में जो संपत्ति आप जोड़ते हैं उसे कैसे पास किया जाए। बस उस विचार को पूरा करने के लिए, पैरामीटर केवल तभी पारित किया जाता है जब आप चयनकर्ता में "yourFunctionToNextView:" के बाद ":" शामिल करते हैं। तो उपर्युक्त उदाहरण बटन.प्रोपर्टी = इंडेक्सपाथ के साथ बढ़ाया जाएगा। फिर, चयनकर्ता की परिभाषा होगी: - (शून्य) yourFunctionToNextView: (UIButton *) प्रेषक {}। फिर इस विधि के भीतर, आप NSIndexPath * indexPath = sender.property जैसे कुछ का उपयोग कर सकते हैं; जहां संपत्ति इंडेक्सपाथ है। – JeffB6688

27

आप इस तरह एक UIBarButtonItem जोड़ना चाहिए आप एक नेविगेशन पट्टी है:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]  
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
    target:self action:@selector(addButtonPressed:)]; 
self.navigationItem.rightBarButtonItem = addButton; 
+0

धन्यवाद। मेरी मदद की। आपके लिए उपरोक्त – DrinkJavaCodeJava

2

स्विफ्ट के लिए

let addButton = UIBarButtonItem.init(barButtonSystemItem: .Add, 
            target: self, 
            action: #selector(yourFunction)) 
self.navigationItem.rightBarButtonItem = addButton 
संबंधित मुद्दे