//Text field for email example
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 100, 245, 31)];
textField.delegate = self;
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.placeholder = @”<Enter Email>”;
textField.backgroundColor = [UIColor whiteColor];
textField.tag = TAG_LOGIN_EMAIL; //E.g. Was set as 100
[self.view addSubview: textField];
[textField release];

Text objects can be targeted for specific types of input, such as plain text, email, numeric entry, and so on. The keyboard style identifies what keys are available on the keyboard and which ones appear by default. The default value for this property is UIKeyboardTypeDefault.

Available types for the keyboards are:

typedef enum {
UIKeyboardTypeDefault,                // Default type for the current input method.
UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person’s name or phone number.
UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIKeyboardTypeDecimalPad,             // A number pad with a decimal point.
#endif

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

} UIKeyboardType;

Available types for the return key are:

typedef enum {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;

You can also control the Capitalization and Autocorrection by:

typedef enum {
UITextAutocapitalizationTypeNone,
UITextAutocapitalizationTypeWords,
UITextAutocapitalizationTypeSentences,
UITextAutocapitalizationTypeAllCharacters,
} UITextAutocapitalizationType;

typedef enum {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
} UITextAutocorrectionType;

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/uid/TP40006896-CH3-SW12

//Stroe user information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"Moses" forKey:@"username"];
[prefs setInteger:42 forKey:@"age"];
[prefs synchronize];

//Getting the stored information
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *username = [prefs stringForKey:@"username"];
NSInteger age = [prefs integerForKey:@"age"];

Storing a password in NSUserDefaults is insecure, since anyone who get access to the iPhone, can view the password.

You can either encrypt it or use keychain.

Checkout http://log.scifihifi.com/post/55837387/simple-iphone-keychain-code

One way is to pass parameters in the set selector method.

I find it more OO to create a new object which derive from UIButton, and add the button needed properties to it.

Example:

//
//  MenuButton.h
//  Trivia
//
//  Created by Eylon Steiner on 1/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MenuButton : UIButton {
int customVal;
}

@property int customVal;

@end

//
//  MenuButton.m
//  Trivia
//
//  Created by Eylon Steiner on 1/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import “MenuButton.h”

@implementation MenuButton

@synthesize customVal;

@end
—–

Set it like:

a1 = [MenuButton buttonWithType:UIButtonTypeCustom];
[a1 setCustomVal:cnt];

And use it like:

- (void)networkAdminButtonPressed:(UIButton *)button {
DebugLog (@”button : %d”, [(MenuButton*) button customVal]);

If you want to add a vertical scroll to your UIView:

In the implementation file:

- (void)viewDidLoad {

UIScrollView* textScrollView = [[UIScrollView alloc]init];
[textScrollView setFrame:CGRectMake(0, 0, 100, 100)];
textScrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
textScrollView.pagingEnabled = YES;
textScrollView.scrollEnabled = YES;
textScrollView.showsVerticalScrollIndicator = YES;
textScrollView.bounces = 1;
textScrollView.delegate = self;
[self.view addSubview:textScrollView];

UILabel* tmp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 15)];
tmp.backgroundColor = [UIColor clearColor];
[tmp setText:@"salvador"];
[tmp setFont:[UIFont systemFontOfSize:15]];
[textScrollView addSubview:tmp];
[tmp release];

CGRect rect = CGRectMake(0, 15, 100, 90);
UIImageView *sep = [[UIImageView alloc] initWithFrame:rect];
[sep setImage:[UIImage imageNamed:@"Man_box.png"]];
[textScrollView addSubview:sep];
[sep release];

rect = CGRectMake(0, 105, 100, 90);
sep = [[UIImageView alloc] initWithFrame:rect];
[sep setImage:[UIImage imageNamed:@"Man_box.png"]];
[textScrollView addSubview:sep];
[sep release];

textScrollView.contentSize = CGSizeMake(100, 180);

[super viewDidLoad];
}

In the header file:

@interface ScrollTestViewController : UIViewController <UIScrollViewDelegate>{

}

if([statusString isEqualToString:@"ABC"]){
    //doSomething;
}

1) Check for a memory leak – Instruments ; Make sure you build your application every time before testing it with instruments since instruments is only run without a build process.

2) Add debugging messages or debug line by line to find where the problem occur – Show debug messages

3) Use NSZombie

4) Go over your code and make sure every alloc / new has one release

5) If you are using code like if (pointer == nil) ; you should also set the pointer to nil right after you release it.

So, you got EXC_BAD_ACCESS and now you want to know how to debug and find the problem.

Using NSZombie might help you and give you more information if you enable it.

e.g.: It might give you the following info : -[NSMutableURLRequest release]: message sent to deallocated instance 0x4b37410

In order to enable it:

  • Double-click an executable in the Executables group of your Xcode project.
  • Click the Arguments tab.
  • In the “Variables to be set in the environment:” section, make a variable called “NSZombieEnabled” and set its value to “YES”.

Usually this error indicate that you try to access for a pointer which is no longer valid.

E.g.: The allocating or de-allocating is wrong.

NSZombieEnabled should not be left in place permanently, as by default no objects will ever be truly deallocated, making your application use tremendous amounts of memory. If you find yourself needing to use it often, you can create a second executable in your project which uses NSZombieEnabled so that you can easily switch between them. Do this by right-clicking the original executable, selecting “Duplicate”, and then setting up NSZombie in the new executable.

More info : http://www.cocoadev.com/index.pl?NSZombieEnabled

Command ~ should work, If not working press on the small triangle above the open file.

PS: If you have MAC in order to move between applications / desktop : Try move up and down on the mouse pad with –THREE– fingers :)

Sometimes your application display name is something like ‘game123 lite’,

but the bundle name can not contain any space.

So, I set the product name as ‘game123Lite’, and changed the ‘Bundle display name’ in the info.plist to ‘game123 Lite’.

Most of the games can be separated into several MVC parts.

For example you can look into http://www.appsamuck.com/day12.html:

There is the main game view (MainView…) and the info page view (FlipsideView),

The RootView… is taking care for switching between them and show the first one (main).

Toggeling between the views usually done by something like:

- (IBAction)toggleView {
/*
This method is called when the info or Done button is pressed.
It flips the displayed view from the main view to the flipside view and vice-versa.
*/
if (flipsideViewController == nil) {
[self loadFlipsideViewController];
}

UIView *mainView = mainViewController.view;
UIView *flipsideView = flipsideViewController.view;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];

if ([mainView superview] != nil) {
[flipsideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainView removeFromSuperview];
[infoButton removeFromSuperview];
[self.view addSubview:flipsideView];
[self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewController viewDidDisappear:YES];
[flipsideViewController viewDidAppear:YES];

} else {
[mainViewController viewWillAppear:YES];
[flipsideViewController viewWillDisappear:YES];
[flipsideView removeFromSuperview];
[flipsideNavigationBar removeFromSuperview];
[self.view addSubview:mainView];
[self.view insertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}

Further reading and detailed example : http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/

© 2011 W2OM - iPhone by code Suffusion theme by Sayontan Sinha