简体   繁体   English

试图让静态3D触摸快速动作在Obj-C中工作

[英]Trying to get Static 3D touch Quick Actions to work in Obj-C

So I want a pretty basic 3D touch setup. 所以我想要一个非常基本的3D触摸设置。 On my main UI, I have a 2 option segmented control and I basically want to have one option of it selected upon open when 3DQuickActionA is used, and the other option when 3DTouchQuickActionB is used. 在我的主UI上,我有2个选项的分段控件,我基本上希望在使用3DQuickActionA时在打开时选择一个选项,而在使用3DTouchQuickActionB时选择另一个选项。

I've looked at other questions on this site, and one suggested I use: 我在该网站上查看了其他问题,并建议使用以下问题:

- (void)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem  {
if([shortcutItem.type isEqualToString:@"3DQuickActionA"]){
    self.quote_opt.selectedSegmentIndex = 0;    }
if([shortcutItem.type isEqualToString:@"3DQuickActionB"]){
    self.quote_opt.selectedSegmentIndex = 1;    }
}

where quote_opt is the name of my segmented control. 其中quote_opt是我的分段控件的名称。

However, this doesn't work. 但是,这不起作用。 My app launches ok, but just has whatever the last value of quote_opt was as the current option-- the 3D touch actions do nothing. 我的应用程序启动正常,但是quote_opt的最后一个值是当前选项是什么— 3D触摸操作什么也不做。 I'm sure I'm missing something, but I don't know what. 我确定我想念什么,但我不知道。 Does something need to go in viewdidload? 是否需要在viewdidload中添加内容?

Any advice would be appreciated, and I'm happy to post whatever other portions of code/answer any other questions needed to solve the problem. 任何建议将不胜感激,我很乐意张贴代码的任何其他部分/回答解决问题所需的任何其他问题。

Thank you! 谢谢!

Note: Before this, you should check if handleShortCutItem is called, and if-cases are working as expected, and self.quote_opt is not nil. 注意:在此之前,您应该检查handleShortCutItem是否被调用,并且if-cases是否按预期工作,并且self.quote_opt不为nil。

Another Note: 3D's are generally handled in AppDelegate , is this done as such? 另一个注意事项:3D通常在AppDelegate处理,是这样吗?

Last Note: You may want to take a look at: 最后说明:您可能要看一下:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

Since 3D actions are handled right after the app is opened, it is highly possible that your view elements are not loaded yet. 由于3D动作是在打开应用程序后立即处理的,因此很有可能尚未加载视图元素。 Hence, you need to save the info from your 3D actions and load accordingly. 因此,您需要保存3D动作中的信息并进行相应加载。

You can add a flag in your responsible class and then modify accordingly after the view is loaded. 您可以在负责的类中添加标志,然后在加载视图后进行相应的修改。

An enum for easy switch: 易于切换的枚举:

// You may add additional items to your enum.

typedef NS_ENUM(NSUInteger, ShortcutAction) {
    ShortcutActionNone,
    ShortcutActionA,
    ShortcutActionB,
};

Handling shortcut: 处理快捷方式:

- (void)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem
{
    if([shortcutItem.type isEqualToString:@"3DQuickActionA"])
    {
        self.shortcutAction = ShortcutActionA;
    }
    else if([shortcutItem.type isEqualToString:@"3DQuickActionB"])
    {
        self.shortcutAction = ShortcutActionB;
    }
    // self.shortcutAction will be defaulted to 0 -> ShortcutActionNone.
}

Handling Segment Switch: 处理网段切换:

// You should put this in the interface of your class.

@property ShortcutAction shortcutAction;

// And the implementation:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if(!self.quote_opt)
    {
        // You have other problems, the quote_opt is not initialized yet, or properly.

        NSLog(@"ERR");
    }

    switch(self.shortcutAction)
    {
        case ShortcutActionNone:
        {
            // no-op
            break;
        }
        case ShortcutActionA:
        {
            self.quote_opt.selectedSegmentIndex = 0;

            break;
        }
        case ShortcutActionB:
        {
            self.quote_opt.selectedSegmentIndex = 1;

            break;
        }
    }
}

You also need to check the launchOptions in didFinishLaunchingWithOptions . 您还需要检查didFinishLaunchingWithOptionslaunchOptions

So, as the result of the ongoing chat, here's the latest code: 因此,作为正在进行的聊天的结果,这是最新的代码:

AppDelegate AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [FIRApp configure];

    if(launchOptions)
    {
        UIApplicationShortcutItem *selectedItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];

        if(selectedItem)
        {
            [self applyShortcutItem:selectedItem];
        }
    }
    return YES;
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    [self applyShortcutItem:shortcutItem];
}

- (void)applyShortcutItem:(UIApplicationShortcutItem *)shortcutItem
{
    ViewController *rootViewController = (ViewController *)[self.window rootViewController];

    if([shortcutItem.type isEqualToString:@"DogModeShortcut"])
    {
        [rootViewController setShortcutAction:LaunchDogMode];
    }
    else if([shortcutItem.type isEqualToString:@"CatModeShortcut"])
    {
        [rootViewController setShortcutAction:LaunchCatMode];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM