self presentViewController:viewController animated:YES not working

时间:2016-07-14 07:39:52

标签: ios objective-c cocos2d-iphone cocos2d-x google-signin

我没有使用任何种类的豆荚(不使用可可豆荚)

我已将AppController设置如下:(这是我的viewController

@interface AppController : UIViewController <UIApplicationDelegate, GIDSignInDelegate , GIDSignInUIDelegate>

然后我打电话:[[GIDSignIn sharedInstance] signIn]; 然后:

[self presentViewController:viewController animated:YES completion:nil];

创建问题并向我显示错误:

  

其视图不在窗口层次结构中!

或者,如果我删除UIViewController并将其替换为UIResponder,则错误:

  

UIDelegate必须是| UIViewController |或实施   |登入:presentViewController:|和| signIn:dismissViewController:|   方法来自| GIDSignInUIDelegate |

我只想展示google sign-in UI

我失踪了什么? 我在控制器上做错了吗? 我真的需要帮助。 我的目标c几乎可以忽略不计。 我没有找到任何与 cocos2dx google登录相关的代码。 那里只有快捷的代码。

这是我的代码,我犯了错误:

AppController.h文件代码。

// AppController.h
#import <UIKit/UIKit.h>

#import <GoogleSignIn/GoogleSignIn.h>
#import <GoogleSignIn/GIDAuthentication.h>
#import <GoogleSignIn/GIDGoogleUser.h>
#import <GoogleSignIn/GIDProfileData.h>
#import <GoogleSignIn/GIDSignInButton.h>
#import <GoogleSignIn/GoogleSignIn.h>

@class RootViewController;


@interface AppController : UIViewController <UIApplicationDelegate, GIDSignInDelegate , GIDSignInUIDelegate> {
    UIWindow *window;
    RootViewController* viewController;
}

@property(nonatomic, retain) UIWindow *window;
@property(nonatomic, readonly) RootViewController* viewController;

@end

AppController.mm文件代码。

//-------------------------------------------------
// AppController.mm


#import "AppController.h"
#import "platform/ios/CCEAGLView-ios.h"
#import "cocos2d.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "HelloWorld_ios.h"


//#import <GoogleSignIn/G>

@implementation AppController


static NSString * const kClientID =@"884216498839-g2j0jlq70k249m2ri3lchp4bq1sf1qmb.apps.googleusercontent.com";

#pragma mark -
#pragma mark Application lifecycle

// cocos2d application instance
static AppDelegate s_sharedApplication;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [GIDSignIn sharedInstance].delegate = self;
    [GIDSignIn sharedInstance].uiDelegate = self;
    [GIDSignIn sharedInstance].clientID = kClientID;
    [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES;

    cocos2d::Application *app = cocos2d::Application::getInstance();
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();

    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];

    CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
                                         pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
                                         depthFormat: cocos2d::GLViewImpl::_depthFormat
                                  preserveBackbuffer: NO
                                          sharegroup: nil
                                       multiSampling: NO
                                     numberOfSamples: 0 ];

    [eaglView setMultipleTouchEnabled:NO];

    _viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    _viewController.wantsFullScreenLayout = YES;
    _viewController.view = eaglView;

    // Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: _viewController.view];
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:_viewController];
    }

    [window makeKeyAndVisible];

    [[UIApplication sharedApplication] setStatusBarHidden:true];

    // IMPORTANT: Setting the GLView should be done after creating the RootViewController
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
    cocos2d::Director::getInstance()->setOpenGLView(glview);

    app->run();

    [[GIDSignIn sharedInstance] signInSilently];

    return YES;
}


- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary *)options {

    return [[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                      annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}

// For ios 8 ....

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    return [[GIDSignIn sharedInstance] handleURL:url
                               sourceApplication:sourceApplication
                                      annotation:annotation];
}



- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
}

- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {

}

- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController
{
    [self presentViewController:viewController animated:YES completion:nil];
}

// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn
dismissViewController:(UIViewController *)viewController {

    [self dismissViewControllerAnimated:YES completion:nil];

}





- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {


    cocos2d::Application::getInstance()->applicationDidEnterBackground();
}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    cocos2d::Application::getInstance()->applicationWillEnterForeground();
}

- (void)applicationWillTerminate:(UIApplication *)application {

}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

}


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


@end

HelloWorld_ios.h文件代码。

// =============================================
// HelloWorld_ios.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <GoogleSignIn/GoogleSignIn.h>
#import <GoogleSignIn/GIDAuthentication.h>
#import <GoogleSignIn/GIDGoogleUser.h>
#import <GoogleSignIn/GIDProfileData.h>
#import <GoogleSignIn/GIDSignInButton.h>
#import <GoogleSignIn/GoogleSignIn.h>
#include "AppController.h"

@interface HelloWorld_ios :  UIViewController <GIDSignInUIDelegate, GIDSignInDelegate,UIAlertViewDelegate>
{//@interface HelloWorld_ios


}

- (void)initialize;

@end

HelloWorld_ios.mm代码在这里调用登录 按下按钮。

// =============================================
// HelloWorld_ios.mm


#import "AppController.h"
#import "AppDelegate.h"
#import "HelloWorld_ios.h"
#import "../../proj.ios_mac/ios/AppController.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#ifdef __OBJC__
#import "IOSNDKHelper.h"
#endif
#endif

@implementation HelloWorld_ios

//C++ Call via NDK
- (void)initialize
{
    [IOSNDKHelper addNDKReceiver:self moduleName:@"receiver_LocalNotifications"];
    CCLOG("test5 in the intiallize()...ios...");


}

- (void) CallingForSignIn:(NSObject*) params
{
    NSMutableDictionary *mapData = (NSMutableDictionary*) params;
    NSString* receiveData = [mapData objectForKey:@"testData"];
    NSLog(@"This is test1 NDK %@",receiveData);

//    [[GIDSignIn sharedInstance] signOut];
//    [[GIDSignIn sharedInstance] signInSilently];
    [[GIDSignIn sharedInstance] signIn];

}


- (void) CallingForSignOut:(NSObject*) params
{
    NSMutableDictionary *mapData = (NSMutableDictionary*) params;
    NSString* receiveData = [mapData objectForKey:@"testData"];
    NSLog(@"This is test1 NDK %@",receiveData);

    [[GIDSignIn sharedInstance] signOut];

}


@end

2 个答案:

答案 0 :(得分:0)

“其视图不在窗口层次结构中!”问题几乎是由于:

您有现在的视图控制器(尚未呈现)并尝试在其上方显示另一个视图。

答案 1 :(得分:0)

您是否已将String mTitleBody = Html.fromHtml(postBodyText).toString().substring(0, 150).trim(); mTitleBody = mTitleBody.concat("..." + mContext.getResources().getString(R.string.readMore)).replaceAll("<img.+?>|<IMG.+?>", "").replaceAll("\n", "<br/>"); int index1 = Html.fromHtml(mTitleBody).toString().trim().length() - mContext.getResources().getString(R.string.readMore).length(); int index2 = Html.fromHtml(mTitleBody).toString().trim().length(); postTextView.setTextIsSelectable(true); postTextView.setMovementMethod(LinkMovementMethod.getInstance()); postTextView.setText(Html.fromHtml(mTitleBody), TextView.BufferType.SPANNABLE); Spannable mySpannable = (Spannable) postTextView.getText(); ClickableSpan myClickableSpan = new ClickableSpan() { @Override public void onClick(View view) { Log.d("FirstClass", "onClick"); Intent intent = new Intent(mContext, SecondActivity.class); (mContext).startActivity(intent); ((Activity) mContext).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); ds.setColor(ContextCompat.getColor(mContext, R.color.body_text_3)); } }; mySpannable.setSpan(myClickableSpan, index1, index2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 添加到rootviewcontroller?错误消息很清楚地抱怨它。