I'm adding dynamic 3D touch quick actions to my iOS app.
I was able to setup the code that displays each action, but I am having trouble finding what code would go within the code below. When the action is chosen, I need the action to open up one of 4 different view controllers within my app.
if ([shortcutItem.type isEqualToString:#"addOpportunity"]) {
} else if ([shortcutItem.type isEqualToString:#"bookMark"]) {
} else if ([shortcutItem.type isEqualToString:#"searchGuest"]) {
} else if ([shortcutItem.type isEqualToString:#"myGuest"]) {
}
else {
}
}
First you will have to create the instances of the view controllers that you intend to show in each of the if conditions.
For example:-
SearchGuestViewController * vc = [[SearchGuestViewController alloc] init];
Next you can present the view controller modally by doing the following:-
[self presentViewController:vc animated:YES completion:nil];
Note: You can only present a view controller from within another view controller.
This is one way of launching a view controller.
Read more about launching view controllers in the link below.
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html
Related
im have two view controller UserListView and UserProfileView!
in UserListView view controller i'm have a button for swtich to UserProfileView and here is code.
UserListView.m - Click Action
- (IBAction)SettingClick:(id)sender
{
UserList *UserProfile = [self.storyboard instantiateViewControllerWithIdentifier:#"UserProfileView"];
[self presentViewController:UserProfile animated:YES completion:nil];
}
And code working fine, when user switch to profile (UserProfileView) have a close button back to UserListView and here is code.
UserProfileView.m - Close click action
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
UserProfile *UseList = [self.storyboard instantiateViewControllerWithIdentifier:#"UserListView"];
[self presentViewController:UseList animated:YES completion:nil];
}
in this code i will using [self dismissViewControllerAnimated:YES completion:nil]; to close UserProfileView view controller for low ram usage and it work.
But affter i close UserProfileView i want to open this view controller again and it do not work, UserProfileView not showing again??
i using xcode 5 and building an App for ios 7, please help.
Thanks for your time.
If I understand correctly, when you call SettingClick: your app is displaying a UserList. So, when you dismiss a view controller presented on top of it, you should go back to UserList without the need for presenting it again. So you can try with:
- (IBAction)CloseClick:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
This will work unless you had originally presented UserList as well. In this case, UserList will be dismissed with the top controller. In this case, you can delay presenting a second time UserList after dismissing UserProfile, and it should work.
In the latter case, I would suggest you to use a navigation controller instead of simply presenting your controllers like you are doing. As you see, it is not really straightforward and you will get into catches of any kind. Presenting a controller works ok when you present just one controller at a time. On the other hand, if you instantiate a UINavigationController, this will handle the controllers' hierarchy for you.
use this -
- (IBAction)CloseClick:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
This will return to your previous view when u click that button that has been linked with this action
I have one original view controller with four destination view controllers. I want to be able to push segue with a navigation controller to ALL of the destination view controllers from the original. I have tried...
- (IBAction)notificationsButtonPushed:(id)sender {
NotificationsViewController *notifications = [[NotificationsViewController alloc]init];
[self.navigationController pushViewController:notifications animated:YES];
}
- (IBAction)messagesButtonPushed:(id)sender {
MessagesViewController *messages = [[MessagesViewController alloc] init];
[self.navigationController pushViewController:messages animated:YES];
}
- (IBAction)settingsButtonPushed:(id)sender {
if (canMessage) {
SettingsViewController *settings = [[SettingsViewController alloc]init];
[self.navigationController pushViewController:settings animated:YES];
}
else {
NSLog(#"Can't Message");
}
}
- (IBAction)previewButtonPushed:(id)sender {
PreviewViewController *preview = [[PreviewViewController alloc]init];
[self.navigationController pushViewController:preview animated:YES];
}
and this just gives me an empty view controller without my UI components.
Note: I also have tired "initWithNidName:" and passed in the storyboardID of each destination view controller and it gives me Error:
'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/B7E025E5-D7D2-4FFD-B49C-E10DF5E94C44/LifePoints.app> (loaded)' with name 'preview'
I also have tried... (with storyboard segues set to "Push")
- (IBAction)notificationsButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"notifications" sender:self];
}
- (IBAction)messagesButtonPushed:(id)sender {
if (canMessage) {
[self performSegueWithIdentifier:#"messages" sender:self];
}
else {
NSLog(#"Can't Message");
}
}
- (IBAction)settingsButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"settings" sender:self];
}
- (IBAction)previewButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"preview" sender:self];
}
Although this does push the destination view controllers onto the screen with the appropriate segue type, it does not segue to the correct destination view controller. It only seems to segue to the last attached storyboard segue.
Does anyone know how to correctly apply this and have it behave in the form I am looking for?
EDIT
It is important to note I am checking to see if a condition is met the "messagesButtonPushed" method. I am checking to see if user is allowed to message, and if they are, then segue to the VC.
You don't need any code to implement the basic segues.
In your story board ensure that your original view controller is embedded in a navigation controller (Select the original View and select Edit->Embed in->Navigation Controller).
Then you can simply control drag from each of your four buttons to the corresponding destination views, selecting "push" as the segue type. You can then click on the segue icon between the two views to give each segue an identifier.
You can delete your IBAction methods and any actions on the buttons that link to them.
I suggest you complete a Storyboard Tutorial to learn how storyboards and segues work.
If you do want to perform a segue programatically then you can control drag from the yellow view controller icon at the bottom of the view to the destination. You can then invoke the segue with performSegueWithIdentifier as per your second code - In your case your could have two different segues and trigger the store segue or the other one depending on purchase status
I have view controller I want to load different web pages on button clicks.
I have single view controller where i can load web page.
Button click events
- (IBAction)btnResortTVTouch:(id)sender {
GlobalWebViewController *globalWebViewController1 = [[GlobalWebViewController alloc] init];
globalWebViewController1.strUrlName = #"http://www.youtube.com/user/xyz";
[self presentViewController:globalWebViewController1 animated:YES completion:nil];
[globalWebViewController1 selectPageLink];
}
- (IBAction)btnPIntrestTouch:(id)sender {
GlobalWebViewController *globalWebViewController = [[GlobalWebViewController alloc] init];
globalWebViewController.strUrlName = #"http://www.pinterest.com/xyz/";
[self presentViewController:globalWebViewController animated:YES completion:nil];
[globalWebViewController selectPageLink];
}
It gives error
2013-12-19 03:00:17.885 RWNewYork[5941:907] Warning: Attempt to present GlobalWebViewController: 0x80c8e90 on FiveViewController: 0x81af490 which is already presenting GlobalWebViewController: 0x80826e0
You probably copied the first button, that is how you created the second one in the storyboard, and now it has 2 separate actions. So for one of your buttons both methods are called. Right-click on them in the storyboard and you can see the actions attached.
Edit:
Maybe you should call the selectPageLink method from the completion block of the presentation.
It's telling you that you've already presented a GlobalWebViewController from your FiveViewController, and you're trying to present another GlobalWebViewController from it as well, which isn't possible. So your button press must be somehow calling both methods, or calling one of them twice.
I have a situation where two view controllers are loaded on top of the initial view controller, and when the third view controller is loaded I would like to dismiss the two view controllers for the action of a button on the third view controller. Right now the button is only dismissing one view controller with the following code,
- (IBAction)logout:(id)sender {
[serial close];
if([self.view isKindOfClass:[ViewControllerCreate class]] ) {
[self dismissModalViewControllerAnimated:YES];
}
[self dismissModalViewControllerAnimated:YES];
}
I don't need to dismiss two view controllers every time, just when this particular situation presents itself.
For clarity sake, there is a button in the first view controller which presents the second view controller when pressed, then when the second view controller is loaded, there is a button when pressed presents the third view controller.
You should consider using dismissViewControllerAnimated:completion: which would allow you to chain multiple dismisses. Dismiss the first, pass in a completion to check for the necessary requirements to dismiss the second, etc.
I now have the desired behavior using the following code,
- (IBAction)logout:(id)sender {
[serial close];
if([self.presentingViewController isKindOfClass:[ViewControllerCreate class]] ) {
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];
}
[self dismissModalViewControllerAnimated:YES];
}
Let me explain. I have multiple UIViewControllers. On my MainPageController, I have 3 UIViews. Let's enumerate it this way: the first UIView is called LoginView, the second is called HomeView and the other one is called RegView. Now in HomeView, there are multiple buttons that will lead to other UIViewControllers. For example, one button will lead to StoreController. Now if I am inside StoreController and I want to go back to MainPageController, I simply call:
[self dismissModalViewControllerAnimated:YES completion:nil]
This will send me back to the HomeView.
That is good. However, inside the StoreController, there are buttons which will supposedly direct me to LoginView or RegView, whichever button was tapped. The problem is when the method [self dismissModalViewControllerAnimated:YES completion:nil], it only take me back to HomeView, no matter which button I pressed.
So how will I display the right UIView once the dismissModalViewControllerAnimated is called?
EDIT:
This is how I show the UIViews:
-(void)viewDidLoad
{
//Initialize the views here...
}
-(void)showViewByTag:(NSInteger)tag
{
if (tag == 1)
{
[self.view addSubview:loginView];
}
else if (tag == 2)
{
[self.view addSubview:homeView];
}
else
{
[self.view addSubview:regView];
}
}
Now I call the method showViewByTag: somewhere in my code to display the views.
What you could try and do is following: before calling [self dismissModalViewControllerAnimated:YES completion:nil] (and thus go back to your home view), change the view currently displayed in your MainPageController:
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
If you are worried at the cast and you foresee that self.presentingViewController might be not of MainPageController type on some occasions, then you can check explicitly for its type:
if ([self.presentingViewController isKindOf:[MainPageController class]])
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
For this to compile, MainPageController.h must be imported in your modal controller class.
dismissModalViewController will always bring back the viewController which presented it ,and that can be only one,so the ideal way would be to tell the navigationController to initWith your desired viewController..
eg on regButton click in the presented modalview
RegViewController *regViewController = [[RegViewController alloc]initWithNibNam:#"RegViewController" bundle:nil];
[self.navigationController initWithRootViewController:regViewController];