views:

29

answers:

1

This is my first post on SO, so hi!

I'm new to Xcode and Obj-C too so don't be too harsh.

I am following the demo from Standford University youtube.com/watch?v=L-FK1TrpUng For some reason i've hit an error. Rather than starting from scratch i'd prefer to figure out where I have gone wrong.

Ok so here goes.

I have two view controllers and i'm currently learning about push and pop.

My first view controllers (firstViewController.h) header:

    #import <UIKit/UIKit.h>       
@interface FirstViewController : UIViewController {
    }
     - (IBAction)pushViewController:(id)sender;
    @end

Then initially this was set up in the implementation file (firstViewController.m), like this

#import "firstViewController.h"
    @implementation FirstViewController
    - (IBAction)pushViewController:(id)sender{
}

At this point with IB I ctrl dragged from 'File's owner' to a 'UIButton' and connected 'pushViewController'

However, somewhere along the way I received somekind of error, which I ignored.

Now I have my second view controller added into my firstViewController.m like this;

#import "firstViewController.h"
#import "secondViewController.h"

@implementation FirstViewController

    - (IBAction)pushViewController:(id)sender{
     SecondViewController *secondViewController = [[SecondViewController alloc] init];
     secondViewController.title = @"Second"; 
     [self.navigationController pushViewController:secondViewController animated:YES];
     [secondViewController release];
    }

The error that I received previously seemed to somehow stop me from ctrl dragging from a textLabel in my secondViewController nib

(secondeViewController.h)

#import "firstViewController.h"
#import "secondViewController.h"


@implementation FirstViewController
- (IBAction)pushViewController:(id)sender{
 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 secondViewController.title = @"Second";
 [self.navigationController pushViewController:secondViewController animated:YES];
 [secondViewController release];
}

So i removed the references from my original UIButton via right clicking on it in the firstViewController.xib.

Now I cannot recreate the link from 'File's Owner' to the 'UIButtons', 'pushViewController' outlet (is it an outlet or is it an action?) nor create the link in my secondViewControllers nib from 'File's Owner' to the 'UILabel'.

Any help?

Project files here if anyone is interested. http://zer-o-one.com/upload/files/PushPop.zip

Much appreciated.

A: 

An outlet is a path that connects one object to another. An action is a method name that is called on a particular object in response to an event. Cocoa traditionally uses target/action quite a lot for communicating, though this is now being partly displaced by blocks.

Anyway, your project:

The firstViewController.xib incorrectly thinks that its file owner is a class of type 'firstViewController'. It's actually of type 'FirstViewController' — like most programming languages, Objective-C is case sensitive for class names. In Interface Builder, open firstViewControlller.xib, select 'File's Owner', open the inspector and head to the 'i' tab, then correct the class name at the top. When you've done that, try switching to the connections tab (the one with the arrow pointing to the right) and you should see that it's correctly found your class and your IBAction. You should then be able to control drag.

Basically the same comment applies to secondViewController.

In case you're curious, Objective-C differs from e.g. C++ in that all class names are known at runtime and it's possible to instantiate a class from the string version of its name. That's how XIBs/NIBs are loaded.

Tommy
Sorry; realised I didn't make my complete point. The class name is looked up at runtime, so it's the class name that needs to match. Source code names become irrelevant after compilation, so are irrelevant to the way that Interface Builder operates.
Tommy
Awesome! I spent a good 2 hours on this yesterday and this morning after reading your comment I had solved it after about 2 minutes! I also had to reconnect 'File's Owner' back to view in both my first and second view controllers (the app was launching but once I hit the UIButton it crashed. I managed to figure out why it was crashing after running the debugger and correct the links in IB). Thank you so much for helping! Long live StackOverFlow.
iamneuron