تحميل PresentModalViewController في ViewDidAppear يسبب exc_bad_access

StackOverflow https://stackoverflow.com/questions/2296908

  •  21-09-2019
  •  | 
  •  

سؤال

في follwoing viewControllerClass أحصل على exc_bad_access عند محاولة استدعاء PresentModalViewController في طريقة ViewDidAppear.

#import "SounderViewController.h"
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequest.h"
#import "JSON.h"
#import "InfoViewController.h"


@implementation SounderViewController

@synthesize ipod;
@synthesize ivc;
@synthesize title_lb, artist_lb, check;

-(IBAction)showCurrentSongInfo{

    MPMediaItem * song = [ipod nowPlayingItem];
    NSString * title   = [song valueForProperty:MPMediaItemPropertyTitle];
    NSString * artist  = [song valueForProperty:MPMediaItemPropertyArtist];


    title_lb.text = title;
    artist_lb.text = artist;
}

-(void)playbackStateChanged: (NSNotification*) notification{
    [self showCurrentSongInfo]; 
    NSLog(@"Playback state: %@",[notification name]);
    if (ipod.playbackState != MPMusicPlaybackStatePlaying) {
        NSLog(@"Is not playing");
        [self presentModalViewController:self.ivc animated:YES];
    }else if (ipod.playbackState == MPMusicPlaybackStatePlaying) {
        NSLog(@"Is playing");
        [self dismissModalViewControllerAnimated:YES];
    }
}

-(void)nowPlayingItemChanged: (NSNotification*) notification{
    [self showCurrentSongInfo]; 
    NSLog(@"Playing item changed: %@",[notification name]);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.ivc = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];   

    self.ipod = [MPMusicPlayerController iPodMusicPlayer];


    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector (playbackStateChanged:)
                                                 name:@"MPMusicPlayerControllerPlaybackStateDidChangeNotification"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector (nowPlayingItemChanged:)
                                                 name:@"MPMusicPlayerControllerNowPlayingItemDidChangeNotification"
                                               object:nil];  
    [[MPMusicPlayerController iPodMusicPlayer] beginGeneratingPlaybackNotifications];

} 

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    if (ipod.playbackState != MPMusicPlaybackStatePlaying) {
        [self presentModalViewController:self.ivc animated:YES];
    }else{
        [self showCurrentSongInfo]; 
    }
}

-(IBAction)showInfoView{ 
    [self presentModalViewController:self.ivc animated:YES];
}



#pragma mark View Methods

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

استدعاء الطريقة

 [self presentModalViewController:self.ivc animated:YES];

في ViewDidAppear يسبب exc_bad_access.

لقد جربت تصحيحه مع NSZOMBIEENABLED ، لكنني حصلت على مكالمة فقط إلى MAIN. الشيء الذي يجعلني مجنونًا هو أنه إذا تم تشغيل نفس الكود من طريقة تشغيل MethodStateChanged ، فإنه يعمل بشكل جيد.

إذا كان بإمكان أي منكم مساعدتي ، فلا يمكن أن أشعر بالجرأة بالسرعة. شكرًا.

هل كانت مفيدة؟

المحلول

لقد عملت أخيرًا! لكنني أعتقد أنه حل سريع.

لذلك اكتشفت ذلك لجعل IVC الخاص بي لأظهر ، أحتاج إلى تأخير المكالمة إلى PresentModalViewController

[self performSelector:@selector(showWaitingMessageView:) withObject:self.ivc afterDelay:1]; 

هذا هو. إنها تعمل.

لا أعرف لماذا ساعد هذا ، لذلك إذا كان أحدكم المعلمين يعرف المزيد عن ذلك ، فيرجى تنويرني.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top