Archive

Posts Tagged ‘gui’

Close this Number Keypad I shall not see!

  • Did you ever use an iPhone app with a Number Keypad to edit numbers only fields?
  • Did you ever wonder how you could close the keypad, just like you would do with the standard keyboard and its ‘Done’ button?
  • Are you sure you want to know the answer…?

YOU CAN’T! (at least if the application just used the standard Number Keypad provided within iOS SDK)

So, what choice do you have?

  • Generally, the keypad closure is not really necessary, since the edited field is in its own screen, you just let the user go back (generally through the navigation bar) and the keypad is closed automatically.
  • You may imagine other solutions, too…
  • But the one I’ll give you here offers the following result:
Simulateur iPhone
Uploaded with plasq‘s Skitch!

Now that you’ve seen this, you wan’t to know how to do it, don’t you? So here’s the howto! It’s not my own creation, I followed their tutorial and used the graphic files provided, so you can do so! I just poured a little bit of idea I found on another blog to make it a little more better (to me at least).

So this idea is just to use the powerfulness of Cocoa’s categories (extending classes without subclassing), to add a method to the UIView class enabling me to find the firstResponder from a view and resign it. Calling this method on the app’s main window when I want to close the keypad, it will seek and find the firstResponder, resign it, and thus close the keypad! Easier, isn’t it? And the corresponding is below…

#import 

@interface UIView ( RoCHExtensions )
- (UIView *)findAndResignFirstResponder;
@end

@implementation UIView (RoCHExtensions)
- (UIView *)findAndResignFirstResponder {
    if (self.isFirstResponder) {
        [self resignFirstResponder];
		return self;
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findAndResignFirstResponder];

        if (firstResponder != nil) {
			[firstResponder resignFirstResponder];
			return firstResponder;
        }
    }

    return nil;
}
@end

Shortest guide to UIScrollView

Want to implement the UIScrollView fast?

  1. Build your scroll view in IB.
  2. Build the content view you wish to scroll.
  3. Link all these things to some class with IBOutlets.
  4. Ask the class (preferably in awakeFromNib) to add your content view as scroll view’s subview.
  5. Last but not least: set your scroll view’s contentSize, or it won’t scroll!
Categories: Cocoa, iPhone SDK Tags: , , , ,

How to add a search bar to your table view, and integrate it in your section indexes?

Assume you have a table view filled with objects. Assume you have sections for these objects, and you have setup the section indexes. Until this point, nothing which cannot be found in SDK’s documentation.

Assume now you want to add a search bar to your table view. What do you do? To answer this question, we will start from source code available to all of us, implementing the required table view with section indexes, we shall use the TableViewSuite’s 3rd example, I have named “3_SimpleIndexedTableView” (you can download the tutorial package there).

From this base, I will indicate the updates necessary to display a search bar in the first row. I will do this the fast way, so it may not be really nice. And I will not implement everything required to do search and display search results, you will have to look at documentation for this (this will essentially consist in connecting the search bar with required objects and delegates, providing content for search results).

So, what do we do to the TableViewSuite’s 3rd example:

  1. We change the numberOfSectionsInTableView: method in order to return one row more (you guess? …the row for the search bar naturally!):
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count] + 1; // CHANGE: add one for the search cell
    }
  2. We update (a little) the tableView:numberOfRowsInSection: and tableView:titleForHeaderInSection: methods in order to have it return good number of rows for the good sections:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 1; // CHANGE: return one for the search cell
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section - 1];
    return [timeZonesInSection count];
    }
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) return nil;
    return [[collation sectionTitles] objectAtIndex:section - 1];

    }
  3. Finally, we update the most important method, I have named tableView:cellForRowAtIndexPath: :
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // CHANGE: add the search cell
    if ([indexPath indexAtPosition:0] == 0) {
    static NSString *CellIdentifier = @"SearchCell";
    UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [searchBarCell addSubview:[[UISearchBar alloc] initWithFrame:searchBarCell.frame]];
    return searchBarCell;
    }

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Get the time zone from the array associated with the section index in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section - 1];
    // Configure the cell with the time zone's name.
    TimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZone.localeName;
    return cell;
    }
  4. Like I told before, you should then get the searchBarCell connected to the good objects to have it up and working. One last tip, I use the UISearchDisplayController class and connect them both in this way:
    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
    [sdc setValue:self forKey:@"searchResultsDataSource"];
    [sdc setValue:self forKey:@"searchResultsDelegate"];

Uh oh, I almost forgot to give you the best tip! How to have the “search” tool icon (what’s this tool’s name in English?) in your section indexes? Just follow the guide… (’cause this cannot be found in the doc, but you may find it elsewhere on the net, like I did – however it’s too long ago, I have not kept the source link).

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[NSArray arrayWithObject:@"{search}"] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
}

If you have a problem, you can’t make it work in your code, do not hesitate to ask… I have tried this code changes and it worked for me, but I may have forget something!

[Edited] As requested, please find the xCode project there.

How to open sheets and start modal sessions?

To open sheets, or start modal sessions for a given window, use the shared instance of NSApplication.

Categories: Cocoa Tags: , ,

How to control the creation of an untitled document at application’s launch?

This is determined by the delegate’s methods for NSApplication:

  • applicationShouldOpenUntitledFile:
  • applicationOpenUntitledFile:
Categories: Cocoa Tags: , , ,
%d bloggers like this: