0

I got a problem with a universal app that were initial design for the iPhone and I used a 3rd party class for a popup on the iPhone. on the iPad I want to use the Apple provided popup. My problem is my code is written so that the View Controller for the popup already is initialized in the viewDidLoad of the class containing the popup, and when seguing - a new instance of that class is allocated. Is there a way to pass it the bckMusicPlayer that´s already been allocated in prepareForSegue?

@property (nonatomic,strong) MJDetailViewController *bckMusicPlayer; //Already initialized when prepareForSegue get´s called. I want to segue to this object.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepare for segue");
    if ([segue.identifier isEqualToString:@"Show iPad Player"]) {
        if ([segue isKindOfClass:[MJDetailViewController class]]) {

        }

    }
}

edit: I got it working passing it the player (AVAudioPlayer) object from bckMusicPlayer

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Show iPad Player"]) {

        NSLog(@"class: %@",[segue class]);
        if ([segue.destinationViewController isKindOfClass:[MJDetailViewController class]]) {
            NSLog(@"prepare for segue");
            MJDetailViewController *destinationVC = segue.destinationViewController;
            destinationVC.player = self.bckMusicPlayer.player;
        }

    }

}

Cezar
  • 55,636
  • 19
  • 86
  • 87
Tom Lilletveit
  • 1,872
  • 3
  • 31
  • 57

2 Answers2

1

No, you can't do that. Segues (other than unwinds) always instantiate new controllers. You either need to not instantiate it in the viewDidLoad, or move to the new controller in code rather than using a segue.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

Can't you go the other way round? Why do you have to create an instance of MJDetailViewController? Let the segue do it for you and then in prepareForSegue: you grab the destination controller and pass it the required data. Don't fight the framework.

Alternatively you could override:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

And prevent the segue in case it is targeting MJDetailViewController and manually transfer to your controller and present it.

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Do you have any links on how to do that - manually transfer to a controller? – Tom Lilletveit Jul 31 '13 at 16:24
  • @TomLilletveit What do you mean? Using a UIPopoverController? You will find plenty examples if you google that on Bing or on SO. – Krumelur Jul 31 '13 at 16:25
  • see my updated question! It seems to be working fine, thought it is a hack, but it´s due to me being lazy and putting stuff that should be in the model in the view controller – Tom Lilletveit Jul 31 '13 at 16:56
  • You did exactly what I proposed. Read my answer: "then in prepareForSegue: you grab the destination controller and pass it the required data" – Krumelur Jul 31 '13 at 18:10