2011-05-11 19 views
32

मैं एक निब के बिना एक UITableViewController उपयोग करने के लिए चुना है। मुझे दो बटनों के साथ नीचे एक UIToolbar की आवश्यकता है। ऐसा करने का सबसे आसान तरीका क्या है?कैसे एक UITableViewController प्रोग्राम के रूप में करने के लिए एक UIToolbar जोड़ने के लिए?

पीएस मुझे पता है कि मैं आसानी से एक UIViewController का उपयोग करें और जोड़ने के एक UITableView लेकिन मैं चीजों को एप्लिकेशन पर एक जैसी दिखना चाहते हैं कर सकते हैं।

किसी की मदद कर सकते हैं?

मैं निम्न उदाहरण देखा है और मैं इसकी वैधता पर यकीन नहीं है:

(void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    //Initialize the toolbar 
    toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault; 

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit]; 

    //Caclulate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height; 

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.parentViewController.view.bounds; 

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds); 

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds); 

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight); 

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea]; 

    //Create a button 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back" 
                    style:UIBarButtonItemStyleBordered 
                    target:self 
                    action:@selector(info_clicked:)]; 

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]]; 

    //Add the toolbar as a subview to the navigation controller. 
    [self.navigationController.view addSubview:toolbar]; 

    [[self tableView] reloadData]; 
} 

(void) info_clicked:(id)sender { 

    [self.navigationController popViewControllerAnimated:YES]; 
    [toolbar removeFromSuperview]; 

} 
+0

है कि सबसे महत्वपूर्ण लाइन यहाँ बात करने के लिए की आवश्यकता है [उपकरण पट्टी sizeToFit]; इसके बिना - टूलबार प्रदर्शित होता है, लेकिन किसी भी उपयोगकर्ता इंटरैक्शन को स्वीकार नहीं करता –

उत्तर

78

सरल बात करने के लिए एक UINavigationController के शीर्ष पर अपनी परियोजना का निर्माण करना है। इसमें पहले से ही एक टूलबार है, यह डिफ़ॉल्ट रूप से छिपा हुआ है। आप toolbarHidden संपत्ति टॉगल करके यह प्रकट कर सकते हैं, और अपने तालिका दृश्य नियंत्रक जब तक यह नेविगेशन नियंत्रक पदानुक्रम में है इसका इस्तेमाल करने में सक्षम हो जाएगा।

अपने अनुप्रयोग प्रतिनिधि में, या वस्तु में अपने ऐप्लिकेशन के प्रतिनिधि के नियंत्रण से गुजरता है, जड़ दृश्य नियंत्रक के रूप में अपने UITableViewController साथ नेविगेशन नियंत्रक बनाने के लिए:

- (void)application: (UIApplication *)application 
      didFinishLaunchingWithOptions: (NSDictionary *)options 
{ 
    MyTableViewController   *tableViewController; 
    UINavigationController  *navController; 

    tableViewController = [[ MyTableViewController alloc ] 
           initWithStyle: UITableViewStylePlain ]; 
    navController = [[ UINavigationController alloc ] 
          initWithRootViewController: tableViewController ]; 
    [ tableViewController release ]; 

    /* ensure that the toolbar is visible */ 
    navController.toolbarHidden = NO; 
    self.navigationController = navController; 
    [ navController release ]; 

    [ self.window addSubview: self.navigationController.view ]; 
    [ self.window makeKeyAndVisible ]; 
} 

फिर अपने MyTableViewController वस्तु में टूलबार आइटम सेट करें:

- (void)viewDidLoad 
{ 
    UIBarButtonItem   *buttonItem; 

    buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back" 
              style: UIBarButtonItemStyleBordered 
              target: self 
              action: @selector(goBack:) ]; 
    self.toolbarItems = [ NSArray arrayWithObject: buttonItem ]; 
    [ buttonItem release ]; 

    /* ... additional setup ... */ 
} 
+5

यह सलाह का एक अद्भुत टुकड़ा है। मुझे कभी नहीं पता था कि UINavigationController में एक टूलबार है जो डिफ़ॉल्ट रूप से छिपा हुआ है। मैं पहले से ही इसका इस्तेमाल कर रहा था इसलिए यह एक वास्तविक बोनस था। वास्तव में सबकुछ समझाने के लिए समय निकालने के लिए भी धन्यवाद। – jini

6

आप नेविगेशन नियंत्रक विशेषताओं निरीक्षक में "टूलबार दिखाता है" विकल्प भी देख सकते हैं।

+0

लेकिन फिर यह उन सभी विचारों पर चालू है जो कोड का उपयोग करने में समाप्त होते हैं, वैसे भी उन दृश्यों पर जहां टूलबार का उपयोग नहीं किया जाना चाहिए। –

1

यहाँ एक सरल उदाहरण है, जो मदद मिल सकती है

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteMessages)]; 
    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeMail)]; 
    NSArray *toolbarItems = [NSMutableArray arrayWithObjects:spaceItem, trashItem,spaceItem,composeItem,nil]; 
    self.navigationController.toolbarHidden = NO; 
    [self setToolbarItems:toolbarItems]; 

धन्यवाद, prodeveloper

संबंधित मुद्दे

 संबंधित मुद्दे