10

I am relatively new to Objective-C and I am coming from a Java background, thus I find the notion of pointers and not-that-well explained compilation errors quite daunting.

I have a class that implements a delegate with the jsonControllerDidLinkedInAuth method. Whenever the delegator send a message to this class, passing itself and a (NSURL *)url, I am trying to assign the url to a property authUrl, when this operations finishes call the performSegueWithIdentifier, which in turn executes the prepareForSegue (as far as my understanding of this is concerned).

This doesn't seem to work, as I am receiving a:

App[3278:13517] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setUrlAddress:]: unrecognized selector sent to instance 0x6acab00'

error message that terminates the application with SIGABRT.

Both of the methods reside in the same class, which is a UINavigationController and points to another UINavigationController. The compiler points me at the [vc setUrlAddress:self.authUrl]; of the second method.

- (void)jsonControllerDidLinkedInAuth:(JsonController *)controller :(NSURL *)url {
    authUrl = url;
    if (authUrl != nil) {
        [self performSegueWithIdentifier:@"LinkedInAuth" sender:self];
    } else {
        NSLog(@"Auth URL is null");
    }
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"LinkedInAuth"]) {
        // Get destination view
        LinkedInViewController *vc = [segue destinationViewController];
        NSLog(@"The auth url from prepareForSegue in SignupViewControlle: %@", self.authUrl);
        [vc setUrlAddress:self.authUrl];
    }
}

What seems to be the problem is that for some reason I cannot access the authUrl property of the class I currently am. I tried with authUrl, [self authUrl] and self.authUrl but none of this seem to do the job. Properties are synthesized, but I have also noticed a little hint: "UINavigationController setUrlAddress", shouldn't this be a LinkedInViewController like defined in the method above? I tried casting (LinkedInViewController)[segue destinationViewController], but then I get a message notifying me that you cannot cast onto a pointer.

Any help would be highly appreciated. Thanks for reading.

Edit 1: The NSLog just above the setUrlAddress does return the self.authUrl. I am assuming it must be some sort of a visibility problem, but I am not sure how to solve this.

4

2 回答 2

21

如果您的视图控制器嵌入在 UINavigationController 中,segue.destinationViewController则指的是该导航控制器。这是你可以使用的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        if ([[segue identifier] isEqualToString:@"LinkedInAuth"]) {
            UINavigationController *navigationController = (UINavigationController *)segue.destinationViewController;
            LinkedInViewController *linkedInViewController = [[navigationController viewControllers] lastObject];
            linkedInViewController setUrlAddress:self.authUrl];
        }
}

如果您有超过 1 个转换为嵌入 UINavigationController 的视图控制器的 segue,我通常将其添加到开头prepareForSegue:sender

if ([segue.destinationViewController isKindOfClass:[UINavigationController subclass]])
        id vc = [[(UINavigationController *)segue.destinationViewController viewControllers] lastObject]

现在vc包含您真正感兴趣的视图控制器,因此您不必为每个 segue 复制和粘贴铸件。

于 2012-06-29T19:24:34.897 回答
0

看起来您并没有分配委托,您需要这样做才能使委托模式起作用。尝试添加vc.delegate = self到您的 prepareForSegue。

于 2012-06-29T19:42:32.337 回答