Follow the images to see how is linked Inteface with the code in an iPhone Application.
Download UnexpectedIT iPhone Xcode Project
title="view4" width="700" height="432" class="alignnone size-medium wp-image-290" />
Follow the images to see how is linked Inteface with the code in an iPhone Application.
Download UnexpectedIT iPhone Xcode Project
title="view4" width="700" height="432" class="alignnone size-medium wp-image-290" />
//** 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];
//** 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> {
NSArray *listData;
NSIndexPath * selectedIndexPath;
int selectedRow;
}
@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];
}
//The class declaration always begins with the @interface compiler directive and ends with the @end directive.
//MyClass.h
@interface MyClass : NSObject
{
int count;
NSString* name;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int count;
-(MyClass*)thisIsAMethod:(NSString*)aName;
@end
//MyClass.m
@implementation MyClass
@synthesize name;
@synthesize count;
+ (void) initialize {
self.name=[NSString stringWithFormat:@"Something"];
}
-(MyClass*)thisIsAMethod:(NSString*)aName {
NSLog(aName);
return self;
}
@end
//** Declared Properties **
@property BOOL flag;
@property (copy) NSString *nameObject; // Copy the object during assignment.
@property (readonly) UIView *rootView; // Declare only a getter method.
@property (retain) NSString *name; //Specifies that retain should be invoked on the object upon assignment.
@property (nonatomic) NSArray *array; //Specifies that accessors are non-atomic. By default, accessors are atomic.
//You can combine the @property in a single line if you want:
@property (nonatomic, assign) int number1, number2, number3;
//Note: Each readable property specifies a method with the same name as the property. Each writable property specifies an additional method of the form setPropertyName:, where the first letter of the property name is capitalized.
//In your class implementation, you can use the @synthesize compiler directive to ask the compiler to generate the methods according to the specification in the declaration:
@synthesize flag;
@synthesize nameObject;
@synthesize rootView;
//You can combine the @synthesize statements in a single line if you want:
@synthesize flag, nameObject, rootView;
// UIAction and IBOutlet
// IBAction and IBOutlet are macros defined to point variables and methods to the Interface Builder.
@interface FirstView : UIViewController {
IBOutlet UITextField *personFilter; //this object will be able to point to a Text Field Input in the Interface
//[...]
}
-(IBAction) touchSearch:(id)sender; //this method will be able to point to an event in the Interface
@end
//** Methods and Messaging Syntax **
//Method declaration syntax:
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
// - is method type identifier
// (void) is Return type
// insertObject, atIndex: Method signature keyworks
// id, NSUInteger: Parameter types
// anObject, index: Parameter names
//Example
-(NSString *) getFilterName:(SearchFilters) filter
//Example
- (NSString *) getName:(NSString *)thisArray
atIndex:(int)index
//** Messaging **
//When you want to call a method, you do so by messaging an object.
//Messages are enclosed by brackets ([ and ]). Inside the brackets, the object receiving the message is on the left side and the message (along with any parameters required by the message) is on the right.
[myArray insertObject:anObject atIndex:0];
//To avoid declaring numerous local variables to store temporary results:
[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0];
//Objective-C also provides a dot syntax for invoking accessor methods:
[myAppObject.theArray insertObject:[myAppObject objectToInsert] atIndex:0];
//You can also use dot syntax for assignment:
myAppObject.theArray = aNewArray;
//The syntax for a class method declaration is identical to that of an instance method, with one exception. Instead of using a minus sign for the method type identifier, you use a plus (+) sign.
//** Constructor **
+ (void) initialize {
// Initialization for this class and any subclasses
}
//Strongly and weakly typed variable declarations:
//When storing objects in variables, you always use a pointer type. Objective-C supports both strong and weak typing for variables containing objects. Strongly typed pointers include the class name in the variable type declaration. Weakly typed pointers are used frequently for things such as collection classes, where the exact type of the objects in a collection may be unknown.
MyClass *myObject1; // Strong typing
id myObject2; // Weak typing
More info: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/
//The Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming.
//Objective-C extends the standard ANSI C language by providing syntax for defining classes, and methods, as well as other constructs that
//promote dynamic extension of classes.
.h //Header files.
.m //Source files. This is the typical extension used for source files and can contain both Objective-C and C code.
.mm //Source files. A source file with this extension can contain C++ code in addition to Objective-C and C code
//** Enum **
//declaration
typedef enum numberTypes
{
ONE,
TWO,
THREE
} Numbers;
//use
Numbers x = TWO;
int num = (int)THREE;
//Null object is nil
NSMutableArray *myArray = nil; // nil is essentially the same as NULL
/**
*
* Strings
*
*/
// Create a String.
NSString *myString;
NSString *myString2 = @"My String\n";
NSString *myString3 = nil;
// Concatenate numbers and other strings
NSString *anotherString = [NSString stringWithFormat:@"Number: %d and String: %s", 1, @"My String"];
//Double to String: %f 64-bit floating-point number (double)
//Char to String: %c
//int to string
[NSString stringWithFormat:@"%d", number];
//or
[[NSNumber numberWithInt:number] stringValue];
// Create an Objective-C string from a C string
NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];
//String to Number
//String to Int
int x = [strNumber intValue];
//String to Float
float x = [strNumber floatValue];
//String to Double
double x = [strNumber doubleValue];
/**
*
* Arrays
*
*/
// Create a new array .
NSArray *myArray = [[NSArray alloc] initWithObjects: @"Cat",@"Dog",@"Horse",@"Sheep", nil];
//Array of Arrays
NSMutableArray *searchFilters = [[NSMutableArray alloc] init];
// Initialize with Objects
NSArray *filters = [[NSArray alloc] initWithObjects: @"Cat",@"Dog",@"Horse",@"Sheep", nil];
[searchFilters addObject:filters];
filters = [[NSArray alloc] initWithObjects: @"White",@"Black",@"Red",@"Yellow", nil];
[searchFilters addObject:filters];
filters = [[NSArray alloc] initWithObjects: @"£0 - £20",@"£15 - £40",@"£40 - £80",@"+ £80",@"Any price", nil];
[searchFilters addObject:filters];
/**
*
* Dictionary / Hash Collections
*
*/
//Create the dictionary
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//Add data
[dictionary setObject:@"Toys" forKey:@"34"];
[dictionary setObject:@"Kitchen" forKey:@"45"];
[dictionary setObject:@"Electronics" forKey:@"46"];
//Get Data
NSLog([dictionary objectForKey:@"34"]);
//Get Keys ordered
NSArray* sortedKeys = [[myDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
//Get Values (Note: It will be unsorted)
NSArray* values = [myDict allValues];
/**
*
* Memory Management
*
*/
//Use of the objects if Garbage Collection is supported and activated (e.g.: iPhoneMac OS X supported)
NSString *str = [NSString string];
// Use of the objects if Garbage Collection is not supported (e.g.: iPhone)
MyObject *obj = [[MyObject alloc] init];
[...]
[obj release];
//Note: EXEC_BAD_ACCESS error usually happens when trying to access an object released.
// Only release objects that you retain, copy, alloc, or new.
/**
*
* Protocols and Delegates
*
*/
//A protocol declares methods that can be implemented by any class. Protocols are not classes themselves.
//They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes,
//your class is said to conform to that protocol.
@protocol MyProtocol
- (void)myProtocolMethod;
@end
© 2010 unexpected[it]. All Rights Reserved.
This blog is powered by Wordpress and a modified version of Magatheme