Tag Archives: iPhone

iPhone – Disable the Cut/Copy/Paste Menu on UITextField

Update @ 2012-07-11: for ios >= 5.1 canPerformAction:(SEL)action withSender:(id)sender is not working anymore. Please refer to the following post for more information
StackOverflow – How do you REALLY remove Copy from UIMenuController

 

If you want to disable the Cut/Copy/Paste Menu, add the following piece of code in the implementation file of the view controller which containing the UITextField
Continue reading iPhone – Disable the Cut/Copy/Paste Menu on UITextField

iPhone – Capture Screenshot Programmatically

Capturing screenshot on iPhone needs the QuartzCore framework.

1. Right click the Framework folder in your Xcode project.

2. Add -< Add Existing Frameworks

3. Choose the QuartzCore.framework Continue reading iPhone – Capture Screenshot Programmatically

iPhone – Set Maximum Length of String in UITextField

There is no maximum length attribute in the UITextField. Here is a work around to limit the number of input characters in an UITextField.

1. Implement the UITextFieldDelegate in the .h header file

...
@interface HelloViewController : UIViewController <UITextFieldDelegate> {
	...
}
@end

 

2. Implement this function in the .m implementation file to set the maximum length to 4 characters

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
	NSUInteger newLength = [textField.text length] + [string length] - range.length;
	return (newLength > 4) ? NO : YES;
}

 

Done =)

Reference: iPhone SDK: Set Max Character length TextField

iPhone – Add and Remove Subviews

The layout of an UIView in iPhone Application can be created and modified either by the Interface Builder or by code. If you make it by Interface Builder, you have to wire the items u created to File Owner.

On the other hand, u can make an Item on the UIView by code. The following example will created a “Hello World” UILabel at coordinate (10, 15) with size equals to 40 x 20;

// Create the UILabel instance
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 40, 20)];
// Set the tag of aLabel
aLabel.tag = 1;
// Set the text
aLabel.text = @"Hello World";
// Add the UILabel view to UIView associated with the controller
[self.view addSubview:aLabel];
// Release the memory
[aLabel release];

 

You can add different items such as UIImageView, UITextField etc. Please note that the view tag is the only field to distinguish different subviews.

To remove all the subviews except the one with tag equals to 1, you can use

for (UIView *subview in [self.view subviews]) {
	// Only remove the subviews with tag not equal to 1
	if (subview.tag != 1) {
		[subview removeFromSuperview];
	}
}

 

Done =)

Update @ 2011-06-18: Added the “Release the memory” code. Thanks Luke. =)

iPhone – Accessing the Global Variables in the AppDelegate

Sometimes u would need to create some Global Variables in an iPhone Application such that they can be accessed in other implementation class files.

Those Global Variables are created in the AppDelegate. If you want to access it in another file, you have to get the AppDelegate instance before you can read or write the Global Variables. For example, the HelloAppDelegate has a NSString instance variable named as greeting and i want to read it in the HelloViewController.m
Continue reading iPhone – Accessing the Global Variables in the AppDelegate

iPhone – Start Developing Your First iPhone Application

Recently, I start learning the iPhone Application Development. If you want to be a iPhone Application Developer, you have to fulfill the following requirements.

  • A Mac computer
  • Register as iPhone Application Developer at developer.apple.com
  • Download the Xcode which is your development IDE

 
Continue reading iPhone – Start Developing Your First iPhone Application