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;
      break;
    }
  }
}

- (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;
}

7 Comments

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.

http://blog.mikeweller.com/2010/04/iphone-delete-button-image.html

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:
UITableViewCellSeparatorStyleNone
UITableViewCellSeparatorStyleSingleLine — Default
UITableViewCellSeparatorStyleSingleLineEtched

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];
	UISeg
	for (UIView *aView in [myScrollView subviews]){
		NSLog(@"Class: %@", [aView class]);
	}
}
//Returnes 
//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];
		}

, , , ,

2 Comments

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 🙂

2 Comments

Calculator Alpha for iPad released on the App Store

Apple just released Calculator Alpha for iPad on the App Store. It is a calculator/converter for the iPad and it is free of charge.

http://itunes.apple.com/us/app/calcualtor-alpha/id396151092?mt=8

Try it out and let me know what you think of it…

Some of the features are:

  • works in portrait and landscape mode
  • memory functions (add, subtract, recall, delete)
  • elementary functions (x2, xy, sin, cos, log and others)
  • flow display – shows last 3 calculations
  • saves state, so the next time it is opened you are back where you finished

Calculator Alpha for iPad

Leave a comment

iPad Calculator Alpha and KD Jezica website

Hey, it has been a while since I last posted something here on this blog. I have been busy with building my calculator/converter for iPad called Calculator Alpha. I uploaded it to Apple for review, so probably it will be up there on the App Store in couple of days (or maybe a week). It is going to be free and I think it will be competitive with other calculators out there for the iPad.

Calculator Alpha for iPad

I was also busy building a website for my basketball team here in Slovenia. It is now live at http://www.kdjezica.com. It is build on WordPress blogging platform (like this one here, but is hosted on it’s own server). Hopefully we will keep it up to date so everyone will be able to follow what is going on with our basketball team.

On kdjezica.com we will represent all the teams that are part of KD Ježica, from the men’s team that plays in the second Slovenian league (which I am part of) to the youngest members of our club.

New kdjezica.com website

Leave a comment

Social Calc just released on the App Store

My first app that I submitted to the App Store was just approved. It can be found on: Social Calc

Social Calc calculates how two names get along for love, friendship, business, kids and partying.

Algorithm to calculate percentage is what we used when we were kids in elementary school, it was fun then and I guess it is still fun now (after 20 years).

Application consistis of 5 calculators:

  • Love Calculator
  • Friendship Calculator
  • Business Partner Calculator
  • Kids Calculator
  • Party Calculator
 

, , , ,

Leave a comment