开发者

Multiple UIViews in a viewcontoller?

开发者 https://www.devze.com 2023-01-11 18:38 出处:网络
i have a uiviewcontroller. there are 2 buttons at the top of UIview. i want to load 2 different UIViews (small view; width=320, height=200) bottom if the user taps buttons.

i have a uiviewcontroller. there are 2 buttons at the top of UIview.

i want to load 2 different UIViews (small view; width=320, height=200) bottom if the user taps buttons. i have uitableview in each smallview and some controls. thats why i want to handle each uiview with own uiviewcontrollers.

i think i should create Uiview with CGRect and call small uiviewcontroller when button tapped. but i dont know how. thank

how can i handle 开发者_StackOverflowsmall uiviews with their own uiviewcontroller and can i design small uiviews with IB?


This is definitely possible, Apple doesn't recommend it, but for special cases like yours I agree, use it.

My method:

Make the 2 subview table view controllers without xib files, use -awakeFromNib for setup, rather than viewDidLoad, viewDidLoad will not be called on them. (Wait until the next step before setting up the controllers to load your data, you won't be able to debug until that step works)

Now make a third controller with xib (not table view) and open the xib. Add 2 UITableViewControllers from the library into the window with File's Owner, First Responder, etc. in it, then in the inspector go over to the last tab - change their classes to the 2 custom table view controllers you made.

Next drag 2 table views into the main view, and in the connections tab, tie delegate and datasource to one controller for one table view, and repeat with the other. Also, select the controllers, and connect their view outlet to the table views.

Next you should go into the controller for the main view with xib, and make outlets for the table controllers like this:

 @interface class : UIViewController {
      UITableViewController *controller1;
      UITableViewController *controller2;
 }

 @property(nonatomic, retain) IBOutlet UITableViewController *controller1;
 @property(nonatomic, retain) IBOutlet UITableViewController *controller2;

it is important to use the property so that they are retained, and in the viewDidUnload section, nil them out for memory management: self.controller1 = nil;

Now build and go, and if you managed to follow all this, you should have working table views with controllers, ready to be set up.


Use Alex's solution to setup references to your table views (Small tables as you state). Add your buttons to the enclosing UIView, and place code in your button action methods to swap-out/swap-in the proper views based on the button selection.

- (IBAction) buttonSwapViewPressed:(id) sender {
    if (swapInView1) {
      controller1.view.hidden = NO;
      controller2.view.hidden = YES;
    } else {
      controller1.view.hidden = YES;
      controller2.view.hidden = NO;
    }

Your view hierarchy would look something like:

UIView   - Enclosing View
   UIButton - Swap Button
   UITableView - Table View 1
   UITableView - Table View 2
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号