//** Swiching Views **
//From FirstViewController
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
//** Navigation Controller **
//Pushing the First View Controller in App Delegate File
- (void)applicationDidFinishLaunching {
// Create a navigation controller
navController = [[UINavigationController alloc] init];
// Push the first view controller on the stack
MainMenuController *tmpController = [[MainMenuController alloc] initWithNibName:@"MainMenuView" bundle:nil];
[navController pushViewController:tmpController animated:NO];
[tmpController release];
[navController pushViewController:firstViewController animated:NO];
// Add the navigation controller’s view to the window
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
//Push to add a view controller
ViewController *newView = [[ViewController alloc] initWithNibName:@"NewView" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
[newView release];
//Go back / remove / pop to the View before
[self.navigationController popViewControllerAnimated:YES];
//... or pop to the Root View
[self.navigationController popToRootViewControllerAnimated:YES];
//Set to change the entire stack of view controllers
- (void)setViewControllers:(NSArray *)viewControllers
animated:(BOOL)animated;
//** Message Box / Pop up messages **
// Simple
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PopUp!" message: @"..." delegate:self cancelButtonTitle:@"Wooohooo!" otherButtonTitles:nil];
[alert show];
// Extended
NSString *msg = [[NSString alloc] initWithFormat:
@"This is a Alert Box Message, %d, everything went OK."
, 123];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"PopUp!"
message:msg
delegate:self
cancelButtonTitle:@"Wooohooo!"
otherButtonTitles:nil];
[alert show];
[alert release];
// Question Dialog
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Confirm"];
[alert setMessage:@"Do you want to open Safari?"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
// Yes Code
NSLog(@"Yes");
}
else if (buttonIndex == 1) {
// No
NSLog(@"No");
}
}
//** Sharing Data Between Views / Use of a Singleton **
//ClassToShare.h
@interface ClassToShare : NSObject
{
int profileId;
}
@property (nonatomic) int profileId;
+ (ClassToShare *)sharedSingleton;
@end
//ClassToShare.m
@implementation ClassToShare
@synthesize profileId;
+ (ClassToShare *)sharedSingleton
{
static ClassToShare *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[ClassToShare alloc] init];
return sharedSingleton;
}
}
@end
//How To Use
//From AppDelegate.m (don't forget to add #import "ClassToShare.h")
- (void)applicationDidFinishLaunching:(UIApplication *)application {
ClassToShare *session = [ClassToShare sharedSingleton];
session.profileId = 123;
//[...]
}
//From Any View (don't forget to add #import "ClassToShare.h")
ClassToShare *session = [ClassToShare sharedSingleton];
session.profileId; //123
//** UITableView Table Views Controller Code / Checklist Table **/
@interface SelectFilterController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *uiTableView;
NSArray *listData;
NSIndexPath * selectedIndexPath;
int selectedRow;
}
@property (nonatomic, retain) IBOutlet UITableView *uiTableView;
@property (nonatomic, retain) NSArray *listData;
@property (nonatomic, assign) int selectedRow;
@end
@implementation SelectFilterController
@synthesize listData;
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
// [...]
- (void)viewDidAppear:(BOOL)animated {
//tableViewController is a IBOutlet linked to UITableView object in the Interface Builder
//With this you select programmatically a row in the tableview
if(self.selectedRow > -1) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.selectedRow inSection:0];
[self.tableViewController.delegate tableView:self.tableViewController didSelectRowAtIndexPath:indexPath];
}
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow;
//check if selectedIndexPath exists
if(selectedIndexPath != nil)
[selectedIndexPath row];
else
-1;
//select or deselect the check mark
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:selectedIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
selectedIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
//** UIWebView **
- (void)viewDidLoad {
NSString *urlAddress = @"http://www.apple.com/uk";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
//** UITextView **
IBOutlet UITextView *txtNotes;
txtNotes.font = [UIFont fontWithName:@"Helvetica" size: 13];
txtNotes.text = @"Lorem ipsu ...";
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
//** UIBarButtonItem **
/*
Image Buttons
- Toolbar and navigation bar icons: 20 x 20 pixels.
- Tab bar icons: 30 x 30 pixels.
*/
/*
PNG Image with:
- Transparent background
- Pure white with appropriate alpha.
- Do not include a drop shadow.
- Use anti-aliasing.
*/
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Button1.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(doStuff)];
/*
PNG Full Color
*/
image = [UIImage imageNamed:@"Button2.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:button];
/*
Space in between your toolbox buttons
*/
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
/*
Button with image and text
*/
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithTitle:@"Text"
style:UIBarButtonItemStylePlain
target:self
action:@selector(pressButton2:)];
button3.image = [UIImage imageNamed:@"Button3.png"];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: flexItem, button1, flexItem, button2, flexItem, button3, flexItem, button4, flexItem, nil];
//Release buttons
[button1 release];
[button2 release];
[button3 release];
[flexItem release];
/** Table View Controller **/
//Controller
@interface TableController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *uiTableView;
}
#import "MovieTableController.h"
@implementation TableController {
- (void)viewDidLoad {
[super viewDidLoad];
//Customizing Table View
uiTableView.rowHeight = 75;
uiTableView.backgroundColor = [UIColor whiteColor];
uiTableView.separatorColor = [UIColor grayColor];
}
Tags: checklist, message box, navigation controller, switch view, uitableview, uiwebview