Apple iphone SDK TabBars
Note: Updated tutorial and source code offered at
Step 1: Read the actual Hello World tutorial
In case you are are unfamiliar with building in Objective C as well as IPhone SDK please take the
opportunity to go through the sooner tutorialIPhone SDK Hello Globe. It also contains instructio
ns for you to install XCode and the apple iphone SDK.
Step 2: Develop a project
Select File > Brand new Project or Apple Crucial + Shft + N to create up the new task menu. Select the
Applications item from the IPhone OS section through the menu on the remaining, and select Window
Based Application through the icons on the correct. When prompted enter the project name, I used TabBar
in the small sample code. You should possess two files TabBarAppDelegate. l and TabBarAppDelegate. m.
Step three: Create the View Controllers
We will create two tabs and we want a view controller for every one. Hence Create 2 files of type
UIViewController SubClass. We named the files View1Controller. mirielle and View2Controller. m Be sure
you check the box in order to also create the header document.
Now You should have Six files within your project:
TabBarAppDelegate. m
TabBarAppDelegate. l
View1Controller. m
View1Controller. l
View2Controller. m
View3Controller. l
Step 4: Setup the View ControllersView1ControllerFirst we are going to setup the view that contains the
Button and Textual content Field. Browse to the actual View1Controller. h header file and add the next
interface elements, UITextField, UIButton, UIView and also put in a reference to the Application delegate
which is used to communincate among views. We also create properties for each one of the variables we
have described. The final code should look something similar to this:
1 #import
only two #import "TabBarAppDelegate. h"
@class TabBarAppDelegate;
only two @interface View1Controller: UIViewController
3
4 UITextField *textField;
5 UIButton *cmdButton;
6 UIView *myView;
7 TabBarAppDelegate *delegateRef;
8
9
10@property (nonatomic, assign) UITextField *textField;
11@property (nonatomic, assign) UIButton *cmdButton;
12@property (nonatomic, assign) UIView *myView;
13@property (nonatomic, assign) TabBarAppDelegate *delegateRef;
12
15@end
In the View1Controller. m file synthesize the actual properties and write and init strategy to create the user
user interface elements. Technically this should maintain the load view method but since i have am allowing
one see to directly access another I want them to be initlized even if they may not be loaded. The proper way
to get this done is to store information seperately and pull it once the view is loaded but i wish to keep this
tutorial easy.
1 - (id)init
2
3 if (self = [super init])
4
5 // Initialization code
6 self.title = @"Text and Button"; //Set View title which will be displayed in Tab Bar
7 // create and configure the view
8 CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320); //define size and position of view
9 myView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
10 myView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
11 self.view = myView; //set view property ov controller to the newly created view
12
13 // create a UIButton (UIButtonTypeRoundedRect)
14 cmdButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
15 cmdButton.frame = CGRectMake(100, 100, 100, 50);
16 [cmdButton setTitle:@"Click Me" forState:UIControlStateNormal];
17 cmdButton.backgroundColor = [UIColor clearColor];
18 [cmdButton addTarget:self action:@selector(action:)
forControlEvents:UIControlEventTouchUpInside];
19 cmdButton.adjustsImageWhenHighlighted = YES;
20
21 //create a text field
22 cgRct = CGRectMake(60, 170, 200, 50); //define size and position of textbox
23 textField = [[UITextField alloc] initWithFrame:cgRct];
24 textField.text = @"Enter Text Here";
25 textField.borderStyle = UITextBorderStyleBezel;
26
27 //Add text field and button to main view
28 [self.view addSubview:cmdButton];
29 [self.view addSubview:textField];
30
31 return self;
32
Also we need to include an action handler to deal with the button click. Add the next method to
View1Controller. mirielle. Note that view tend to be accessing the label utilizing the view controller that we
reference utilizing the AppDelegate instance pointed to through the delegateRef property.
1 instruction (void)action: (id)sender
2
3 [self.textField resignFirstResponder]; //Hide Keyboard
4 self.delegateRef.view2Controller.label.text = self.textField.text;//Ubdate label
5
View2Controller
Upon tot he second see controller, we do not maintain an app delegate example here because this view
doesn't need to initiate any interaction using the first view. If this were false then we would additionally
keep a reference right here
1 #import
2 @interface View2Controller: UIViewController
3
4 UILabel *label;
5 UIView *myView;
6
8
8 @property (nonatomic, assign) UILabel *label;
9 @property (nonatomic, assign) UIView *myView;
twelve
11@end
In the View2Controller. m file synthesize the actual properties and write and init strategy to create the user
user interface elements.
Step 5: Creating the App Delegate
Within the TabBarAppDelegate. h header file we define cases of the UITabBarController along with the two
views we possess just setup. We produce properties for these factors an also declare this individual relevent
classes and importance the relevent header documents. The final code should look something similar to this:
1 #import
only two #import "View1Controller. h"
a few #import "View2Controller. h"
three @class TabBarViewController;
5 @class View1Controller;
a few @class View2Controller;
7 @interface TabBarAppDelegate: NSObject
8
9 UIWindow *window;
10 UITabBarController *tabBarController;
11 View1Controller *view1Controller;
12 View2Controller *view2Controller;
13
12
15@property (nonatomic, assign) UIWindow *window;
16@property (nonatomic, assign) UITabBarController *tabBarController;
17@property (nonatomic, assign) View1Controller *view1Controller;
18@property (nonatomic, assign) View2Controller *view2Controller;
19@end
Within the implementtion file TabBarAppDelegate. m update the applicationDidFinishLaunching strategy to
create the view controllers and also the tab bar controllers and add these to the window. The code with this
is as follows.
a single @synthesize window;
2 @synthesize view1Controller;
a few @synthesize view2Controller;
4 @synthesize tabBarController;
5 various - (void)applicationDidFinishLaunching: (UIApplication *)application
6
7 //Initilize objects
8 tabBarController = [[UITabBarController alloc] init];
9 view1Controller = [[View1Controller alloc] init];
10 view2Controller = [[View2Controller alloc] init];
11
12 //Add a refernce to self in the View1Controller so that it can reference View2Controller via this object
13 view1Controller.delegateRef = self;
14
15 //Add views to the TabBar
16 tabBarController.viewControllers = [NSArray arrayWithObjects:view1Controller, view2Controller, nil];
17
18 //Add tabbar to window and display
19 [window addSubview:tabBarController.view];
20 [window makeKeyAndVisible];
21
Stage 6: Build and Proceed
Click the Build an Go button to operate your code and you ought to see the Tab Bar using the first view
Loaded. Enter your teck and click on the button on the display screen. Now load the seconf view by clicking
the best Tab and see your own text updated there.
Stage 7: Extra Credit Including Icons
Create two thirty two x 32, 150 dpi, grey scale png images that you would like to use as symbols I called
mine symbol. png and icon1. png. Import the images to the resources folder by correct clicking it and
choosing Add > Existing Documents... and selecting the picture files. Now in the init approach to both
ViewControllers (View1Controller and View2Controller) add the next lines respectively.
self. tabBarItem. picture = [UIImage imageNamed:@"icon.png"];
self. tabBarItem. picture = [UIImage imageNamed:@"icon1.png"];
Click Build and Visit the icons.
Concluding Notice
I hope this guide was clear and helpful allow me to know if there tend to be any problems or if it may be
improved. Also please down load the XCode project resource from my site
UpdateA simpler method of exchanging data between sights, See ()
iphone 4s review