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

Graduation, iOS SDK…

It has been a while since I last posted anything on this blog, and I really should try and post at least once a week. Will try to do so in weeks to come. Since the last time I wrote anything I graduated from University of Ljubljana Slovenia in civil/structural engineering. My final thesis was called: Development of a software tool for creating integrated models in Sketchup.. I defended it on June 29th in Ljubljana, and received a top grade for it. Basically the thesis was written about two software programs I made and shared on this blog. One is OnTime scheduling tool made with Adobe Flex 3.0 Framework and second is OnTime5D, a plugin for Google Sketchup for creating 5D animations of building process inside Sketchup according to a selected building schedule. I would love to enhance both of this programs in the future, so if anyone out here is interested in doing something in this way please drop me a line!

Secondly I would like to share some words on iOS development. I started developing applications for the iPhone and iPad almost a year ago. It was the first time I used any traditional programming language where you actually have to take care of the memory yourself. But I have to say that I like iOS SDK a lot, and was quickly able to build some very interesting applications. The most recent one will soon be sent to Apple for their approval, and hopefully it will be on the App store in about 1 month.

Hopefully see ya in a week 🙂