My app have a main screen (mainView). On mainView, I have a button. When user taps it, I use
[self presentViewController:libraryView animated:YES completion:nil]
to present another view (libraryView). Then, on libraryView, I use
[self.view addSubview:tabBarController.view]
to add a UITabBarController which has 2 View Controllers: featuredBooks and recentBooks
Everything works fine. But when I add a button to featureBooks to dissmiss the libraryView and return to mainView, the following methods don't work
[self dismissViewControllerAnimated: YES completion:nil]
[self.parentViewController dismissViewControllerAnimated: YES completion:nil]
[self.presentingViewController dismissViewControllerAnimated: YES completion:nil]
I know the reason: self refers to featureBooks's view, not libraryView.
So, how do I refer to libraryView, dismiss it and return to mainView from a view controller (featureBooks or recentBooks) inside Tab Bar Controller?
Thank you very much.
Make mainView delegate object of libraryView... Then when you call the delegate method from libraryView, mainView will call in its code the method dismissViewcController.
So:
1)create into libraryView controller .h code:
#protocol LibraryViewDelegate
- (void) LibraryViewDelegate_DismissButtonClicked;
#end
2)then create a property into mainView .h file:
#property(nonatomic, assign) NSObject<LibraryViewDelegate> *delegate;
and the following into the .m one
#synthesize delegate;
and assign mainView to this property after libraryView object creation and before you will present it
3)write the following code into the mainview .m file:
-(void)LibraryViewDelegate_DismissButtonClicked{
//put here the code for dismissing mainView created modalViewController (libraryView)
}
4)then write the code that call:
[self.delegate LibraryViewDelegate_DismissButtonClicked];
into libraryView when you press the dismiss button
Related
I have a main view controller that is able to process gestures in an iPad app.
I launch a second view controller via:
wVC = [self.storyboard instantiateViewControllerWithIdentifier:#"vc_webView"];
[self presentViewController:wVC animated:YES completion:nil];
If I now gesture while that VC is showing, the gestures are not processed. How can I "pass" the gestures to the first storyboard for subsequent processing so that I don't need to rewrite the whole gesture functionality in the new VC?
OK so the answer is to use delegates.
For completion's sake, here is the way to do it:
Set up the first VC to be a delegate of the second, then in the second VC call the delegate function.
In original controller right before presenting the wVC:
wVC.delegate = self;
In the wVC.h file:
#protocol senddataProtocol <NSObject>
-(void)ProcessPasswordGesture:(NSInteger)iGest;
#end
#property(nonatomic,assign)id delegate;
In the wVC.m file:
#synthesize delegate;
[delegate ProcessPasswordGesture:<data>];
Hope this helps someone else!!
Ad the title says, I'm wondering how I move back to a already allocated view. In this case I have three views, Mainmenu, Main_game_view and game_over_view. I want to move back from game_over_view to mainmenu without reallocating it. Also note that I'm using xib files
How do I do that?
If you use storyboard, you can create a segue by control-dragging.
If you move from viewController1 to viewController2 by [viewController1 presentViewController:viewController2 animated:YES completion:nil];, you can use [viewController2 dismissViewControllerAnimated:YES completion:nil]; to go from viewController2 back to viewController1.
If you use a UINavigationController and push viewController2 by [someNavigationController pushViewController:viewController2 animated:YES];, then you can do [someNavigationController popViewControllerAnimated:YES]; to pop the view controller and go back to the first view.
If it's multiple views presented on the same view controller, then you can remove the views from their super view using [someView removeFromSuperview] and leave the first view only.
Hope it helps :)
========
Edit:
You can create a reference of your Mainmenu in your Game_over_view view controller:
In Main_game_scene view controller's .h file, create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Mainmenu creates Main_game_scene view controller, do main_game_scene.mainMenuViewController = self;.
In Game_over_view view controller's .h file, also create #property (strong, nonatomic) UIViewController *mainMenuViewController;. After Main_game_scene creates Game_over_view, do game_over_view.mainMenuViewController = self.mainMenuViewController;.
Now you have a reference of Mainmenu in Game_over_view, just call [self.mainMenuViewController dismissViewControllerAnimated:YES completion:nil];.
I am new to iOS development and I am doing little project as a research.
I have an app where after I start it I am showing MainViewController, but if this is the first launch of this app I want to show Sub1ViewController (names are made up) using presentViewController method called in MainViewController.
After user puts in some data on Sub1ViewController I invoke dismissViewController method to hide it.
The hard part starts here - I have no idea how to capture the event when Sub1ViewController is dismissed and I can present Sub2ViewController also using presentViewController invoked from MainViewController. All the time I am getting messages that I am trying to present view when another present or dismiss is in progress.
PS: I am using Xamarin, but I also understand objective-c.
hi you can try out this
[self dismissViewControllerAnimated:YES completion:^{
//code to be executed with the dismissal is completed
// for example, presenting a vc or performing a segue
}];
you can write code after completion of dismiss one view controller
or
You can achieve this by present view after some delay in dismiss method
-(void)onDismisViewController{
[self performSelector:#selector(presentSecoundViecontoller) withObject:self afterDelay:1];
}
-(void)presentSecoundViecontoller
{
SecoundViewController *secoundVC = [[SecoundViewController alloc] initWithNibName:#"secoundVC" bundle:nil];
[self.navigationController presentViewController:secoundVC animated:YES completion:NULL];
}
EDIT
Try this i Try and it work
crate one delegate Method in subviewcontroller1 and set delegate in mainviewcontroller and implement method in mainvie and present subviewcontroller2 in this delegate method its work
try this if not let me know will post code.
Delegate Creation on subviewcontroller1.h file
#protocol st1Protocol <NSObject>
- (void)presentViewController;
#end
#interface SubViewController1 : UIViewController
#property (nonatomic,assign) id <st1Protocol> delegate;
- (IBAction)dissmiss:(id)sender;
SubViewcontroller1.m file
i put dismiss view on button click of subviewcontroller1 you do this in you dismiss method
- (IBAction)dissmiss:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
//code to be executed with the dismissal is completed
// for example, presenting a vc or performing a segue
[self.delegate presentViewController];
}];
}
now implementation of this delegate method in main view controller.
implement delegate method in .h file
#interface StViewController : UIViewController<st1Protocol>
set delegate in mainviewcontroller.m file view didload
- (void)viewDidLoad
{
[super viewDidLoad];
subViewcontroller1 *st1=[[subViewcontroller1 alloc]initWithNibName:#"subViewcontroller1" bundle:nil];
st1.delegate=self;
[self presentViewController:st1 animated:YES completion:nil];
// Do any additional setup after loading the view from its nib.
}
delegate method implementation in main view controller
-(void)presentViewController{
SubmViewcontroller2 *st2=[[SubmViewcontroller2 alloc]initWithNibName:#"St2ViewController" bundle:nil];
[self presentViewController:st2 animated:YES completion:nil];
}
hope this may help you happy coding.
I'm new to iOS programming and I'm facing a problem
I'm having a problem with custom delegate.
I'm trying to make a simple custom where it return data to the previous view controller and pop the current view controller.
I have 2 navigation view controller
1 - main view controller
2 - Adding
and here is the protocol that is written in the adding view controller
#protocol AddingDelegate <NSObject>
#required
-(void)setInformation:(Adding *)controller withObject:(Conference *)info;
and here is the where I called it in adding view controller
-(IBAction)addingConference
{
NSLog(#"Adding Button Pressed");
conferenceObject = [[Conference alloc]init];
conferenceObject.name = [NameTX text];
conferenceObject.city = [CityTX text];
conferenceObject.description = [Dectription text];
NSMutableArray *info = [[NSMutableArray alloc] init];
[info addObject:conferenceObject];
[self.delegate setInformation:self withObject:conferenceObject];
NSLog(#"adding Conference method is done");
}
I wrote the delegate at the interface in the main view controller
#interface MainViewController : UITableViewController <AddingDelegate>
#end
and here where I declared the delegate method
-(void)setInformation:(Adding *)controller withArray:(NSMutableArray *)info
{
NSLog(#"in the main view at the delegate");
[self.navigationController popToRootViewControllerAnimated:YES];
NSLog(#"Should be popped right now");
}
and this is the prepare for segue method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"AddObject"]) {
UINavigationController *navigation = segue.destinationViewController;
Adding *addingViewController = [[navigation viewControllers]objectAtIndex:0];
addingViewController.delegate = self;
}
}
now the problem is when I push the adding on top of the stack and then fill the information and press done the adding view controller doesn't pop to show main view controller.
I tried to log everything and the logs from the main view controller doesn't show .
Please help me
What I notice here is that in the implementation of prepareForSegue:sender: the segue's destinationViewController is a navigation controller. This makes me think that your segue is not pushing the AddingController on the current navigation stack but it's presenting a new one instead. This means the new navigation controller containing the AddingController is presented modally and as such, when you try to pop the navigation stack nothing seems to happen because you're operating on the wrong navigation stack. If that is the case you have two options: 1. change [self.navigationController popToRootViewControllerAnimated:YES]; for [self dismissViewControllerAnimated:YES completion:nil]; or 2. change the segue to be a push segue instead of a modal segue and point the segue directly to the AddingController.
In Adding.m
#class Adding;
#protocol AddingDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface Adding : UIViewController
#property (weak, nonatomic) id <AddingDelegate> delegate; // have you forgot this one
#end
and use
[self dismissViewControllerAnimated:YES completion:nil];
You need dismiss if you want get back to previous screen and make sure you have added Navigation controller
I have a button in my popover controller. I want to use it to dismiss the popover, so I am trying to access a method (dismissPopover) of the presenting view controller (the "root" view controller).
Note: the method to dismiss the popover is already set up and working, in the root VC, which is the delegate. If I call it it will dismiss the popover. I just need to access the method from the popover.
To do this I set up a property in the AppDelegate, and get an instance of the rootVC like this: self.rootController = (ViewController*)self.window.rootViewController;. Then I imported the root VC class and the AppDelegate to the popover's view controller's class, as below. Seems to give me access to the rootVC, and the methods, but the results do not fire the method. Any idea what I am missing here?
#import "ViewController.h"
#import "AppDelegate.h"
Action connected to button:
- (IBAction)dismissPopover:(id)sender {
//Checking the button works, it does:
NSLog(#"dismissPopover, from popover");
//Trying to get an instance of the rootViewController, the "presenting view controller"
ViewController *rootVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] rootController];
//trying to access the method in the rootVC that dismisses the popover
[rootVC dismissPopover];
//Tried the following code, does nothing:
//[self dismissPopoverAnimated:YES];
}
NOTE: I ended up abandoning the use of a popover for this as it became a bit over complicated. I tried loading my view controller into a UIView (so I could load the contents of a nib to a pop-up view). That also became a bit complicated. So, for now I am just building my desired interface in a UIView programatically. So far works great.
dismissPopoverAnimated: is a method of UIPopoverController class. so, you need a popover controller reference in your 'root' view controller.
MyRootViewController.myPopoverController = thePopover;
the button is in your 'root' view controller, and in it's action method:
[self.myPopoverController dismissPopoverAnimated:YES];
In iOS 8, you can dismiss the popover (if it's coming from a segue, at least) with dismissViewControllerAnimated:completion: from within the popover. Doesn't work in iOS 7 (or below), however.
Popover automatically dismissed when clicking outside it , as you order a button to dismiss it you can simply use the following code inside your dismissPopover method :
[self.popoverController dismissPopoverAnimated:YES];
you don't need all this tedious work !
[self dismissViewControllerAnimated:YES completion:nil];
is the solution;
you just need an IBoutlet or add target to your button and then call above line
I had the same problem
just do in your buttonClickMethod:
[yourPopoverController dismissPopoverAnimated:YES];
hope you help!
cheers