The following is an example of the simplest interface switch in iOS. Multiple controllers are used and code handling is demonstrated between controllers when switching interfaces.
Implementation of the application interface:
First, create a window-based application, which is:
The purpose of using Window-base application is to try to explain the process of programming from the most basic case. Once the project is created, it can be compiled and run, and the result is the White screen page:
Writing the first view and controller, I call it topic, the subject, so the controller is named: Topiccontroller, View Topicview.
Create Topiccontroller:
This will create 3 files:
The view Xib file is also created together. And:
The file's owner's class is automatically generated.
In Mainwindow.xib, add the controller you just created (Topiccontroller).
First drag a view controller in:
Then, rename the View controller:
Next, set the controller to Windowdemoappdelegate. In its header file:
#import <UIKit/UIKit.h>
#import "TopicController.h"
@interface windowdemoappdelegate:nsobject<uiapplicationdelegate> {
Uiwindow*window;
Iboutlettopiccontroller *topiccontroller;
}
@property (nonatomic, retain) Iboutlet UIWindow *window;
@end
In the implementation file:
#import "WindowDemoAppDelegate.h"
@implementation Windowdemoappdelegate
@synthesize window;
#pragma mark–
#pragma mark Application Lifecycle
-(BOOL) Application: (UIApplication *) Applicationdidfinishlaunchingwithoptions: (Nsdictionary *) launchOptions{
Overridepoint for customization after application launch.
[Self.windowaddSubview:topicController.view];
[Self.windowmakekeyandvisible];
Returnyes;
}
Then, to see it clearly, change the color of the view in the Topiccontroller.xib file to green:
To run the app, you'll see the following effect:
To add a button to the interface:
Create a processing method for the button. In the header file of the Topiccontroller:
#import <UIKit/UIKit.h>
@interface Topiccontroller:uiviewcontroller {
}
-(Ibaction) Getdetail: (ID) sender;
@end
In the implementation file:
#import "TopicController.h"
@implementation Topiccontroller
-(Ibaction) Getdetail: (ID) sender{
NSLog (@ "Getdetail ...");
}
In IB, connect the button to the Controller's action:
Run the app again to see what's printed in the log:
Follow the method above to create a controller, and then create a detailcontroller. First set the color of the Detailcontroller.xib view, for later debugging observation recognition.
Then I need to click the Topiccontroller View button to switch to Detailcontroller view. This needs to be managed in the following code:
#import "TopicController.h"
@implementation Topiccontroller
-(Ibaction) Getdetail: (ID) sender{
NSLog (@ "Getdetail ...");
}
The basic idea is to find the window instance, which can be replaced topiccontroller by setting a new controller instance (such as Detailcontroller) through the Rootviewcontroller property of window. The code can be written like this:
#import "TopicController.h"
#import "DetailController.h"
@implementation Topiccontroller
-(Ibaction) Getdetail: (ID) sender{
NSLog (@ "Getdetail ..., window.views:%@", self.view.window.subviews);
Detailcontroller *detailcontroller=[[detailcontroller alloc]initwithnibname:@ "DetailController" bundle:nil];
Self.view.window.rootviewcontroller=detailcontroller;
NSLog (@ "window.views:%@", detailController.view.window.subviews);
}
With this part of the code, click on the button to take effect, resulting in this effect:
The above code explains:
- First, a new Detailcontroller instance is created
- Then, the View property of the current controller, you can get the window instance, with the Rootviewcontroller property of the window instance set, replace the current controller with the new controller
- The Window object is a very important class, you can think of it as the iOS development of the picture frame, the view is placed in the picture frame, window has a subvews list, which can hold multiple view
- When setting the Window.rootviewcontroller property, window automatically adds the Uiviewcontroller view of the property to the Subview of the window, which is why two of the window.subviews lists printed in the log The reason for the instance
This code is very incomplete, for example, there is a memory leak, need this:
Detailcontroller *detailcontroller=[[[detailcontroller alloc]initwithnibname:@ "DetailController" Bundle:nil] Autorelease];
Because, this detailcontroller after this sentence, the counter is 1, and then assigned to the Window.rootviewcontroller attribute, is 2. So here we have to do the automatic release.
The problem with this code is that it seems awkward to create another controller in a controller code. This aspect is prone to problems, on the other hand, the structure of the code is not clear. The following is a delegate mode to the code decoupling, but also for the next step to prepare the return button.
Delegate mode, generally used protocol to achieve. Write a protocol first:
#import <UIKit/UIKit.h>
@protocol switchviewdelegate
-(void) getdetail;
@end
Then, you need to have the Uiapplicationdelegate implementation class implement the Protocol:
#import <UIKit/UIKit.h>
#import "TopicController.h"
#import "SwitchViewDelegate.h"
@interface windowdemoappdelegate:nsobject<uiapplicationdelegate,switchviewdelegate>{
Uiwindow*window;
Iboutlettopiccontroller *topiccontroller;
}
@property (nonatomic, retain) Iboutlet UIWindow *window;
@end
In the implementation class:
#import "WindowDemoAppDelegate.h"
#import "DetailController.h"
@implementation Windowdemoappdelegate
@synthesize window;
#pragma mark–
#pragma mark Application Lifecycle
-(BOOL) Application: (UIApplication *) Applicationdidfinishlaunchingwithoptions: (Nsdictionary *) launchOptions{
Overridepoint for customization after application launch.
[Self.windowaddSubview:topicController.view];
topiccontroller.delegate=self;
[Self.windowmakekeyandvisible];
Returnyes;
}
-(void) Applicationwillresignactive: (uiapplication *) application{
}
-(void) Applicationdidenterbackground: (uiapplication*) Application {
}
-(void) Applicationwillenterforeground: (uiapplication*) Application {
}
-(void) Applicationdidbecomeactive: (uiapplication *) application{
}
-(void) Applicationwillterminate: (uiapplication *) application{
}
#pragma mark–
#pragma Mark Memory Management
-(void) applicationdidreceivememorywarning: (uiapplication*) Application {
}
-(void) Dealloc {
[Windowrelease];
[Superdealloc];
}
-(void) getdetail{
Detailcontroller *detailcontroller=[[[detailcontroller alloc]initwithnibname:@ "DetailController" Bundle:nil] Autorelease];
Self.window.rootviewcontroller=detailcontroller;
}
@end
Also, to add the delegate attribute to the controller, the header file:
#import <UIKit/UIKit.h>
#import "SwitchViewDelegate.h"
@interface Topiccontroller:uiviewcontroller {
id<switchviewdelegate>delegate;
}
@property (Nonatomic,retain) id<switchviewdelegate>delegate;
-(Ibaction) Getdetail: (ID) sender;
@end
Implementation file:
#import "TopicController.h"
#import "DetailController.h"
@implementation Topiccontroller
@synthesize delegate;
-(Ibaction) Getdetail: (ID) sender{
NSLog (@ "Getdetail ..., window.views:%@", self.view.window.subviews);
[Delegategetdetail];
}
The implementation of the effect is similar to the above, but after the introduction of the delegate mode, the code structure is relatively clear, conducive to future maintenance.
Transferred from: http://marshal.easymorse.com/archives/4415
IOS for a simple interface switch