When the program contains multiple views and you need to switch between them, you can use UINavigationController or ModalViewController. UINabigationController switches multiple views through the wizard bar. If the number of views is relatively small and the display field is full screen, it is more appropriate to use ModalViewController (for example, the view that requires user input information will be automatically returned to the previous view after the end ). Today, let's take a look at how ModalViewController is created.
ModalViewController is not a specialized class like UINavigationController. It is ModalViewController after it is specified using the presentModalViewController method of UIViewController.
Here we use the CustomViewController (inherited by UIViewController) made of the last two times to implement the ModalViewController instance.
First, prepare the function when ModalViewController exits. Call the dismissModalViewController: Animated: Method of UIViewController, as shown below:
// Exit ModalViewController when the button is pressed.
-(Void) dismiss :( id) inSender {
// If it is called by an instance other than presentModalViewController, parentViewController will be nil, and the following call is invalid
[Self. parentViewController dismissModalViewControllerAnimated: YES];
}
Next, generate another CustomViewController instance to represent ModalViewController and set its corresponding view to red. Then it is passed to presentModalViewController: Animated: display the view of ModalViewController.
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
Controller = [[CustomViewController alloc] init];
[Window addSubview: controller. view];
[Window makeKeyAndVisible];
// Generate ModalViewController
CustomViewController * controllerB = [[CustomViewController alloc] init];
// Set the view background to red.
ControllerB. view. backgroundColor = [UIColor redColor];
// Display ModalViewController view
[Controller presentModalViewController: controllerB animated: YES];
// The presentModalViewController has been managed by the controller. You can release this instance here.
[ControllerB release];
}
After compilation and execution, the ModalViewController view in the red background is started first, and the normal view in the blue background is restored after the button is pressed.
You can also set the modalTransitionStyle attribute of UIViewContrller before displaying the ModalViewController view so that it is displayed in an animation.
1
ControllerB. modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
The preceding implementation only implements the ModalViewController view function. In addition to the information that the program starts to remind the user, nothing can be done. In addition, because it is included in applicationDidFinishLaunching, it cannot be repeatedly displayed. In addition, the content set on ModalViewController view cannot be reflected in the original view.
Next we will implement these functions.
First, when you exit the ModalViewController view, you must notify the original view. Here we use the Delegate design pattern that is frequently used in iPhone/Cocoa applications (which is also recommended ).
In fact, the Image Selection control class UIImagePickerController provided by the system
Or the ABPeoplePickerNavigationController class in the address book uses the Delegate mode.
Based on the example in the previous section, We append three buttons as green, gray, and cancel.
-(Void) viewDidLoad {
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor blueColor];
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = CGRectMake (100,100,100,100 );
Button. tag = 1;
[Button setTitle: @ "green" forState: UIControlStateNormal];
// Function corresponding to the button event
[Button addTarget: self action: @ selector (dismiss :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
Button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = CGRectMake (100,200,100,100 );
Button. tag = 2;
[Button setTitle: @ "gray" forState: UIControlStateNormal];
// Function corresponding to the button event
[Button addTarget: self action: @ selector (dismiss :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
Button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = CGRectMake (100,300,100,100 );
Button. tag = 0;
[Button setTitle: @ "cancel" forState: UIControlStateNormal];
// Function corresponding to the button event
[Button addTarget: self action: @ selector (dismiss :)
ForControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
}
When the program starts, the ModalViewController view is displayed first. If you press any button, the view is closed. Press the "green" button to set the background to green. When you press the "gray" button, set the background to Gray. Do not do anything when you cancel the operation.
The delegate processing is implemented using the following function. If the inColor parameter is nil, it indicates cancellation.
-(Void) selectColor :( UIColor *) inColor;
The instance of the delegate proxy is represented by the id variable.
@ Interface CustomViewController: UIViewController {
Id colorSelectDelegate;
}
The function for setting this variable is as follows.
-(Void) setColorSelectDelegate :( id) inDelegate {
ColorSelectDelegate = inDelegate;
}
In addition, as shown in viewDidLoad above, the buttons are 0, 1, and 2 respectively. When the button is pressed, different tags are used to send different UIColor instances to colorSelectDelegate.
-(Void) dismiss :( id) inSender {
UIView * view = (UIView *) inSender;
UIColor * requestColor = nil;
If (view. tag = 1)
RequestColor = [UIColor greenColor];
If (view. tag = 2)
RequestColor = [UIColor grayColor];
[ColorSelectDelegate selectColor: requestColor];
}
This does not use UIButton * But UIView * because the tag attribute is defined in the UIView class and must not be converted to the UIButton class.
In addition, this function can also be used outside UIButton.
If you want to check the category of the id, you can use isKindOfClass: method.
The inColor parameter is received to change the background color and disable ModalViewController view.
-(Void) selectColor :( UIColor *) inColor {
If (inColor! = Nil)
Self. view. backgroundColor = inColor;
[Self dismissModalViewControllerAnimated: YES];
}
In addition, before calling presentModalViewController (before displaying ModalViewController view), you need to set the delegate instance.
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
Controller = [[CustomViewController alloc] init];
[Window addSubview: controller. view];
[Window makeKeyAndVisible];
// Create the Controller of ModalViewController view
CustomViewController * controllerB = [[CustomViewController alloc] init];
// Set the background color to red
ControllerB. view. backgroundColor = [UIColor redColor];
// Set the delegated instance
[ControllerB setColorSelectDelegate: controller];
// Display ModalViewController view
[Controller presentModalViewController: controllerB animated: YES];
[ControllerB release];
}
After the program is started, ModalViewController view with a red background is displayed. After you click the green button, the background of the original view turns green. Click the gray button to display the gray background, and click Cancel, the original blue background is displayed.
In this form, the button action is delegated to the Controller of the original view for processing. Set different background colors based on the sent UIColor.
From: Yi Feiyang