Archive for the ‘programming’ Category

XCode Hangs when Attaching to App

Wednesday, January 25th, 2012

XCode 4 would hang while attaching to my iOS app. After some research and trial and error, I eventually resolved the problem. There appears to be a number of different causes and solutions to the problem. In my case, the problem was caused by a folder reference (blue folder) under the Supporting Files. Deleting the folder reference and then adding it back solved the problem.

UIBarButtonItem – Programmatically

Wednesday, October 12th, 2011

There is a great tutorial on how to programmatically create and customized the UINavigationController (including UIBarButtonItem) here.

One issue I had was while Adding items to the right of your Navigation Bar. A bar button item is created like so:


UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle@"My Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething)];
self.navigationItem.rightBarButtonItem = myButton;
[myButton release];

The doSomething above, is a method and it looks like this:


- (IBAction)doSomething:(id)sender
{
NSLog(@"%@", @"My Button was pressed");
}

You might expect this to work, however, when the button is pressed the program will crash with an unrecognized selector error. This is because the signature of the doSomething method has a colon on the end of it: doSomething:

Therefore, the code for creating the UIBarButtonItem should be:


UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle@"My Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)];
self.navigationItem.rightBarButtonItem = myButton;
[myButton release];

Custom iOS UIViewController (Universal)

Wednesday, September 7th, 2011

These are the steps to create a custom UIViewControllers and .XIB files that take the place of the default MainWindow_iPhone.xib and MainWindow_iPad.xib in a Universal application.

Below is the architecture diagram. A custom HelloUniverseViewController will inherit from UIViewController. HelloUniverseViewController will not have a .XIB file. HelloUniverseViewController_iPhone and HelloUniverseViewController_iPad inherit from HelloUniverseViewController and have .XIB files. Functionality that is common between the two devices should be implemented in HelloUniverseViewController. Device-specific functionality should be implemented in the appropriate iPhone or iPad view controller.

Class Diagram

1. Create a new Window-based Application.
– Device Family = Universal

2. In the HelloUniverse folder, add a new UIViewController subclass.
– UNCHECK “Targeted for iPad”
– UNCHECK “With XIB for user interface”
– Save As “HelloUniverseViewController”

3. Edit HelloUniverseAppDelegate.h. Add the bold lines below.

#import <UIKit/UIKit.h>

@class HelloUniverseViewController;

@interface HelloAppDelegate : NSObject <UIApplicationDelegate]]]]>

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HelloUniverseViewController *viewController;

@end

4. Edit HelloUniverseAppDelegate.m. Add the bold lines below.

#import "HelloUniverseViewController.h"

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;

    [self.windowmakeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

iPhone

5. In the iPhone folder, add a new UIViewController subclass.
– Subclass of HelloUniverseViewController
– UNCHECK “Targeted for iPad”
– CHECK “With XIB for user interface”
– Save As “HelloUniverseViewController_iPhone”

6. In the iPhone folder, open MainWindow_iPhone.xib.

7. From the Objects list, drag a View Controller object onto the bar.

Object List

|
|
V

Bar

8. Select newly added View Controller object.

UIViewController button

9. Go to the Identity Inspector and change the Class to HelloUniverseViewController_iPhone.

10. Go to the Connections Inspector and add a New Referencing Outlet, viewController = App Delegate

11. Add a label to the HelloUniverseViewController_iPhone.xib: “Hello Universe iPhone”

iPAD

12. In the iPad folder, add a new UIViewController subclass.
– Subclass of HelloUniverseViewController
– CHECK “Targeted for iPad”
– CHECK “With XIB for user interface”
– Save As “HelloUniverseViewController_iPad”

13. In the iPad folder, open MainWindow_iPad.xib.

14. From the Objects list, drag a View Controller object onto the bar.

Object List

|
|
V

Bar

15. Select newly added View Controller object.

UIViewController button

16. Go to the Identity Inspector and change the Class to HelloUniverseViewController_iPad.

17. Go to the Connections Inspector and add a New Referencing Outlet, viewController = Hello Universe App Delegate i Pad

18. Add a label to the HelloUniverseViewController_iPhone.xib: “Hello Universe iPhone”

19. Save, Build and Run

Click here to download the sample XCode project. It targets iOS 4.3 for the iPhone and iPad.

Custom iOS UIViewController

Wednesday, September 7th, 2011

These are the steps to create a custom UIViewController and .XIB that take the place of the default MainWindow.xib.

1. Create a new Window-based Application.

2. Add a new UIViewController subclass.
– CHECK “With XIB for user interface”
– Save As “HelloViewController”

3. Edit HelloAppDelegate.h. Add the bold lines below.

#import <UIKit/UIKit.h>

@class HelloViewController;

@interface HelloAppDelegate : NSObject <UIApplicationDelegate]]>

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HelloViewController *viewController;

@end

4. Edit HelloAppDelegate.m. Add the bold lines below.

#import "HelloViewController.h"

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;

    [self.windowmakeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

5. Open MainWindow.xib.

6. From the Objects list, drag a View Controller object onto the bar.

Object List

|
|
V

Bar

7. Select newly added View Controller object.

UIViewController button

8. Go to the Identity Inspector and change the Class to HelloViewController.

9. Go to the Connections Inspector and add a New Referencing Outlet, viewController = Hello App Delegate

10. Add a label to the HelloViewController.xib: “Hello World!”

11. Save, Build and Run

Click here to download the sample XCode project. It targets iOS 4.3 for the iPad.