2014-05-25 4 views
3

xcode로 SpriteKit에 앱을 만들었습니다. 게임이 끝나면 점수가 표시되고 점수를 페이스 북에 게시하려면이 기능을 추가하고 싶습니다. 거의 모든 내 코드는 MyScene.m에 있으며 어디서 액세스 할 수 없는지 presentViewController입니다. 내 ViewController.m 파일 만 액세스 할 수 있으므로 Viewcontroller에서 Myscene.m의 인스턴스 메서드를 호출 해 보았습니다.하지만이를 수행 할 방법을 찾을 수 없습니다. 다른 파일에서 메서드를 호출하는 유일한 방법은 +(void)을 사용하는 것입니다.이 메서드는 제가 생각하기에 클래스 메서드입니다.페이스 북에 점수 게시

Myscene.m :

if (location.x < 315 && location.x > 261 && location.y < 404 && location.y > 361) { 
//if you click the post to facebook button (btw, location is a variable for where you tapped on the screen) 

    [ViewController facebook]; 
        } 

ViewController.m :

+(void)facebook{ 

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
     SLComposeViewController *facebook = [[SLComposeViewController alloc] init]; 
     facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 

     [facebook setInitialText:@"initial text"]; 
    } 

    } 
그래서

근무하고 올바르게 페이스 북의 클래스 메소드라고,하지만 난 setInitialText 브래킷 후 [self presentViewController:facebook animated:YES]을 넣을 때, 나 에게이 오류를 준다 : 알 수없는 클래스 메서드를 selector에 'presentViewController : animated :'

presentViewController 인스턴스 메서드에서 있지만 클래스 메서드 또는 내 Myscene 파일에서 해당 메서드를 호출 할 수 없습니다. 다른 파일에서 인스턴스 메서드를 호출하거나 클래스 메서드에서 presentViewController에 액세스 할 수있는 방법이 있습니까? 감사합니다

+0

허용되는 대답은이 문제에 영향을 미치지 않지만 언급 한 오류는 클래스에 인스턴스 메서드를 호출하려는 시도가 원인입니다. 클래스 메소드 ('+ (void) facebook')의'self '는'ViewController * yourViewController'가 아니라'[ViewController class]'를 가리 킵니다. 그리고'ViewController' 클래스는 뷰 컨트롤러를 표현할 수 없습니다. – CloakedEddy

답변

3

View Controller에 대한 참조를 SKScene에 전달하거나 NSNotificationCenter을 대신 사용할 수 있습니다. 나는 후자를 선호한다.

먼저 Social.framework를 프로젝트에 추가했는지 확인하십시오.

가져 오기 당신의보기 컨트롤러의 viewDidLoad에 방법에 그리고 #import <Social/Social.h>

이 코드를 추가하여보기 컨트롤러로 사회적 프레임 워크 :

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

다음 뷰 컨트롤러에이 방법을 추가

-(void)createPost:(NSNotification *)notification 
{ 
    NSDictionary *postData = [notification userInfo]; 
    NSString *postText = (NSString *)[postData objectForKey:@"postText"]; 
    NSLog(@"%@",postText); 

    // build your tweet, facebook, etc... 
    SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
    [self presentViewController:mySLComposerSheet animated:YES completion:nil]; 

} 

SKScene의 적절한 위치에서 (게임, 게임 분실 등)이 코드를 추가하십시오 :

NSString *postText = @"I just beat the last level."; 
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:postText forKey:@"postText"]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreatePost" object:self userInfo:userInfo]; 

위의 코드는 텍스트가있는 NSNotification을 보내고 View Controller에서 지정한 메서드를 실행합니다.

+0

정말 고마워요! 완벽하게 작동했습니다. – user3386154

+0

@ user3386154 - 좋습니다. 그것이 트릭을했다면 받아 들인대로 내 대답을 확인하십시오. 감사. – sangony

관련 문제