Archive for category Objective C 2.0

Detecting when clear is clicked in UISearchBar (X button)

When conforming to UISearchBar delegate we do not get a notification when user clicks the clear text button in the UISearchBar. However, UISearchBar has a subview of type UITextField, and if we conform to UITextFieldDelegate we do get a call when clear text button is clicked in form of:

- (BOOL)textFieldShouldClear:(UITextField *)textField

The problem is that when we conform to UISearchBar protocol, we do not conform to the underlying UITextField’s delegate. What we have to do is set the delegate ourselfs in for example viewDidLoad method. (if you do not have an outlet to the UISearchBar create it, and call it searchBar).

In your header (.h) file do not forget to conform to delegates.
<UISearchBarDelegate, UITextFieldDelegate>

- (void)viewDidLoad {
  //find the UITextField view within searchBar (outlet to UISearchBar)
  //and assign self as delegate
  for (UIView *view in searchBar.subviews){
    if ([view isKindOfClass: [UITextField class]]) {
      UITextField *tf = (UITextField *)view;
      tf.delegate = self;

- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
	[aSearchBar resignFirstResponder];

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    //if we only try and resignFirstResponder on textField or searchBar,
    //the keyboard will not dissapear (at least not on iPad)!
    [self performSelector:@selector(searchBarCancelButtonClicked:) withObject:self.searchBar afterDelay: 0.1];
    return YES;


How to create iPhone delete (red) button – iOS, iPhone, iPad

Here is a good blog post about how to create a red iPhone delete button with iOS SDK.

Leave a comment

How to remove borders or separators between cells in UITableView – iOS, iPhone, iPad

When you add a UITableView to your iOS application, there is always a light gray one pixel separator between each cell or row in UITableView.

You can remove this border by setting UITableView’s separatorStyle property to UITableViewCellSeparatorStyleNone and viola, there is no more separators between cells in UITableView.

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

One thing to note here is that you can do this only for the whole UITableView and not for each individual cell for example inside cellForRowAtIndexPath delegate method.

separatorStyle of UITableView has 3 options:
UITableViewCellSeparatorStyleSingleLine — Default

1 Comment

How to insert BOOL (boolean) value into ManagedObject in Core Data – iOS, iPad, iPhone

If you want to insert BOOL type into core data, you can do it with NSNumber’s member method +numberWithBool:

BOOL myBool = YES;
NSNumber *boolAsNumber = [NSNumber numberWithBool:myBool];
//now you can pass boolAsNumber into you managed object for storage!

If you want to retrieve a BOOL from NSNumber you can use -boolValue.

NSNumber *boolAsNumber = [NSNumber numberWithBool:NO];

BOOL getBool = [boolAsNumber boolValue];

, , , ,

Leave a comment

How to remove all views inside another view – iOS, iPhone, iPad

Let’s say we have a UIScrollView (myScrollView) in our application view. Inside UIScrollView we have couple of different subclasses of UIView (UILabel, UISegmentedControl, UIButton), like shown on the picture below.

Every subclass of UIView has a method subviews, which returns NSArray of all the views that are nested inside the view. Since UIScrollView is the subclass of UIView, we can do the following, for example inside viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
	for (UIView *aView in [myScrollView subviews]){
		NSLog(@"Class: %@", [aView class]);
//Class: UISegmentedControl
//Class: UILabel
//Class: UILabel
//Class: UILabel
//Class: UIRoundedRectButton
//Class: UITextField
//Class: UIImageView
//Class: UIImageView

Now if we wanted to delete all the subviews inside UIScrollView we would do

[aView removeFromSuperview];

and thus our UIScrollView would now be empty.

If we wanted to remove just UILabels from our UIScrollView we would do:

if ([aView isKindOfClass:[UILabel class]]){
			[aView removeFromSuperview];

, , , ,


How to trim NSString in Objective C 2.0 – iOS, iPad, iPhone

When you want to trim a NSString in Objective C you can use NSString’s method stringByTrimmingCharactersInSet and passing it a [NSCharacterSet whitespaceAndNewlineCharacterSet] characterSet. This will trim your NSString from all newline and whitespace characters.

NSString *trimMe = @"    Trim this string      ";
trimMe = [trimMe stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Trimmed String: ++%@++", trimMe);
//returnes Trimmed String: ++Trim this string+++

You can also trim just whitespace by passing [NSCharacterSet whitespaceCharacterSet] or trim just newline characters by passing [NSCharacterSet newlineCharacterSet]

, , , , ,

1 Comment

Hide Status Bar in iPhone application, or change it’s style

What do you do to hide the status bar in iPhone application. If you build your views in Interface builder, you can set the status bar to None, but notice that above status bar menu it says: Simulated Interface Elements. That means, that it will only show a change in Interface Builder but not in a compiled application.

You have two options to hide the status bar, one is by editing info.plist file you have (typically in Resources Group). You can either open it up in TextEdit (or any other text editor) and add this value under dict XML tag:

<true />

Other option is to open info.plist file by double clicking it and adding a new variable to the Information Property List. And under key type (or select from drop down menu) Status bar is initially hidden and check the checkbox (thus setting it’s property to true).

Last option is to do it programmatically inside one of your classes by:

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

By changing BOOL value of setStatusBarHidden to NO, you are able to show the hidden status bar:

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];

If you want to change the style of iPhone status bar you can once again edit info.plist and add for Opaque black style the following:


for Transparent black style (alpha of 0.5) add:


Again you can add status bar style by double clicking info.plist and selecting from dropdown menu Status bar style as Key and selecting prefered style as the value (again from the dropdown).


Leave a comment