Reputation: 387
So I want a pretty basic 3D touch setup. 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.
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.
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. I'm sure I'm missing something, but I don't know what. Does something need to go in 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!
Upvotes: 6
Views: 267
Reputation: 1933
You also need to check the launchOptions
in didFinishLaunchingWithOptions
.
So, as the result of the ongoing chat, here's the latest code:
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];
}
}
Upvotes: 1
Reputation: 1933
Note: Before this, you should check if handleShortCutItem
is called, and if-cases are working as expected, and self.quote_opt
is not nil.
Another Note: 3D's are generally handled in AppDelegate
, is this done as such?
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. Hence, you need to save the info from your 3D actions and load accordingly.
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;
}
}
}
Upvotes: 1