问题
I created a subclass of UIViewController. In it I have two properties of:
@property (nonatomic, retain) IBOutlet UIView *presetsView;
@property (nonatomic, retain) IBOutlet UIView *customView;
I added a new UIView to my .xib and put some UI elements in it. I want to hide one view, and show the other based on when the UISegmentedControl is pressed.
My question is in Interface Builder, my original View that was provided me by IB, has an outlet connected to it already to File's Owner. Because I have my own two views, presets and custom, how do I make the outlet connections in IB?
I tried deleting the original view that was provided by IB and dragged two new UIViews to the canvas. I then connected an outlet to each. When I push my new viewController, I get an error that there is no view for my viewController. Then when I connect the File's Owner to the "view" outlet that shows up for the view I want to show first, the application runs. I wasn't sure if this was the correct way, and why it would be the correct way. Does the ViewController always need a .view property an outlet to it? Is that why I needed to do this? Thanks.
回答1:
Yes every UIViewController has a single root view. It must be able to construct that view when its -loadView
method is called either by loading that view from a nib file (and setting its view
property as a result) or by creating it programmatically.
In your case leave the view
property view alone and don't try to swap that around. This root view will have already have been added to the window and changing the controller's reference to point to some other object will just cause confusion and undefined behavior. Instead add both of your views as subviews of the controller's root view and hide or show then as needed.
回答2:
Write action method for UISegmentedcontrol and implemnt method like below
-(IBAction) selectMessageType {
noResultsPriview.hidden = YES;
//[activityIndicator startAnimating];
switch (msgOptionControl.selectedSegmentIndex) {
case 0:
//code for view1 break;
case 1:
//code for view2 break;
case 2:
//code for view3 break;
case 3;
//code for view4
break;
default:
break;
}
}
回答3:
UIViewController must have a valid UIView so in your case it can be the original
UIView created during the XIB creation. Your two UIView properties are correct. All you have to do is to setup in IB the connection between your presetsView
and the original
UIView so that you can later switch back to it, then you need to connect customView
to your other UIView.
So after you finish your original UIView will have two connections, one as a view of UIViewController (owner) and one as a presentsView
.
Those connections are only pointers so that you can use them to manipulate objects in the UIViewController.
来源:https://stackoverflow.com/questions/8723094/using-uisegmentedcontrol-to-switch-between-two-views