I have one delegate file, one View Controller and one UIPopoverController
My PopoverController is defined in delegate file.
when I click on the view controller's button, the popover is shown.
The view controller also contains one method named "refreshviewnow".
When I click on the PopoverController "submit button", I want to call that refreshviewnow function.
What should I have to write on submit button event ?
You need to make sure and set the delegate property of the PopoverController to the View Controller. Then in the "submit button" you can invoke the method on the delegate.
- (void) submit {
[delegate refreshAction];
}
Good Luck.
I am not sure what you mean by "submit button" (maybe you should post some of the code you are using), but if you are trying to call a method that is in the same controller, you would do it like this:
- (void) methodToDisplayPopover
{
[self refreshViewNow];
}
EDIT:
MySecondView *second = [[MySecondView alloc] init];
[second refreshViewNow];
[second release];
I got the answer now.
add observer in View Controller file for method refreshviewnow.
post the notification when popover is going to dismiss.
Related
I have this code:
-(void)applicationDidBecomeActive:(UIApplication *)application {
JUnlockController *passcodeView = [[JUnlockController alloc] init];
[self.navigationController presentModalViewController:passcodeView animated:YES];
}
The problem is, when i have a modal view controller open in my app, it doesn't appear on top of it. I want to be able to find out which is the current viewcontroller that the user is looking at, so I can display it on top of there.
Instead of:
[self.navigationController presentModalViewController:passcodeView animated:YES];
use:
[self presentModalViewController:passcodeView animated:YES];
If your app is only navigated by that nav controller, you can ask it which viewController is visible:
[self.navigationController.visibleViewController presentModalViewController:passcodeView animated:YES];
Or else you can leverage the UIApplicationDidBecomeActiveNotification by having all ov your view controllers extend a custom class which registers for this on viewWillAppear and unregisters in viewWillDissapear; and in your custom base class you implement the callback which displays your modal.
edit note that this assumes you have a self.navigationController in your app delegate (which you setup). You may need to use something like self.window.rootViewController instead.
I don't want to dismiss popover controller when i touch any view. Is it possible?
I am using following code for displaying popover controller.
UIPopoverController* popoverCamera;
popoverCamera= [[UIPopoverController alloc] initWithContentViewController:videoRecorder];
[popoverCamera presentPopoverFromRect:CGRectMake(cropRectangleButton.frame.origin.x,cropRectangleButton.frame.origin.y,0,0) inView:innerview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
NSLog(#"start recording-->%d",[videoRecorder startVideoCapture]);
[videoRecorder startVideoCapture];
[self presentModalViewController:videoRecorder animated:YES];
[videoRecorder release];
Yes it is possible. Implement the following delegate method:
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return NO;
}
Hope this might help you.
From the Link disable dismissal of uipopoverview controller
When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.
implement the pop over delegate
/* Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the dismissal of the view.
*/
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController;
func popoverShouldClose(_ popover: NSPopover) -> Bool {
return false
}
Try this where you dont want to dismiss the popUp:-
[popOverController dismissPopoverAnimated:NO];
Hope this helps :)
I have a simple app that have 3 views, HomeView, MenuView and GameView.
In the HomeView I have 2 buttons (Menu and Start Game). When the menu button is clicked, I open the MenuView using the following code:
- (IBAction)displayMenu:(id)sender{
MenuView *mv = [[MenuView alloc] init];
[self.view addSubView:[mv view];
[mv release];
}
In the MenuView, I have a button that will allow the user to return to the HomeView. When this button is clicked, I use the following code to return to the HomeView
- (IBAction)returnToHome:(id)sender{
HomeView* hv = [[HomeView alloc] init];
[self.view addSubView:[hv view];
[hv release];
}
The above code is working but is this the correct way of doing it? I was under the impression that when I call the addSubView, the view will be retain so If keep going back and forth between HomeView and MenuView, will i have multiple instance of HomeView and MenuView retained since I keep calling addSubView from each of the view?
Thank you.
You could use the UINavigationController, which will allow you to push UIViewControllers on to the stack.
Using the UINavigationController you will get an nice naviagtionbar in at the top of you screen and the back button.
You can find a nice example here:http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
I found this way the most useful and convenient. When calling the new view use this:
HomeView* hv = [[HomeView alloc] init];
(here you can add a uninavigation controller)
[self presentModalViewController:hv animated:YES];
Then to dismiss this view and go back use this:
[self dismissModalViewControllerAnimated:YES];
#atbebtg:
There is a way to do that, infact there are several, since there not really is a "right way" to do it.
For me this works well:
[[self navigationController] setNavigationBarHidden:YES animated:NO];
This will hide the Navigation Bar, so the user can't go back to the last screen.
The other thing you could do is to create your own subclass of UIViewController and not support the button event, like this:
- (IBAction)done:(id)sender
{
//inform the user, that going back is not possible, for example with UIAlertView
//[self.delegate infoViewDidFinish:self];
}
However, this solution seems a bit odd, because the user expects a existing button to work.
Still, this would work.
Others have given answers that present modal view controllers or build a navigation stack. In most cases I would use one of these approaches. Yet, the simplest way to fix the code in the question is to just remove the menu view from the super view. Something like this:
- (IBAction)returnToHome:(id)sender{
[self.view removeFromSuperview];
}
I have an application where I need to have a "custom" setting page. In my delegate I add a UINavigationController's view to the window with a UIViewController as rootviewcontroller.
In the rootviewcontroller I want to have a button and when I press the button the whole view changes to the settingview that I made. I only need the code to change view.
Thanks for the help in advance.
If Settings View a UIView SubClass then In the Button Action method you can do something like this:
[self.view addChild: settingsView]; //provided that settingsView is already allocated.
If you have written down a separate UIViewController SubClass for Settings then you can do something like this in your Button Action Method:
SettingsViewController *controller=[[SettingsViewController alloc]initWithNibName:#"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
I have a button inside the content of a UIPopoverController. This button runs a method called myAction.
MyAction has the form
- (void) myAction:(id)sender
so, myAction receives the id of the caller button.
Now, inside this method I would like to dismiss the UIPopoverController, but the only thing I have is the ID of the caller button. Remember that the button is inside the UIPopoverController.
Is there a way to discover the ID of the UIPopoverController, given the button ID I already have?
thanks.
Unfortunately no. At least, not within the standard practices. You might be able to travel up the responder stack to find it, but it's a hack, it's buggy, and it's really, really messy.
If you want to dismiss a popover by pushing a button, some place relevant should keep a reference to the popover. Usually that would be the owner of the popover (not the controller showed within the popover). When the button is pressed, it can send a message to the owner controller, which can then dismiss the popover.
You might be tempted to have the controller displayed inside of the popover be the owner of its own popover, but coding this way is brittle, can get messy (again), and may result in retain loops so that neither ever gets released.
You can access the presenting popoverController by accessing "popoverController" with KVC.
[[self valueForKey:#"popoverController"] dismissPopoverAnimated:YES]
I have this working, and I do not think it is a hack. I have a standard split view iPad app. I then added a method on my detail controller (the owner of the pop over) to handle the dismissal.
On the standard split view architechture, both the root and detail view controllers are available via the app delegate. So I bound a button click inside the pop over to call a method which gets the app delegate. From there I call the method on the detail controller to dismiss the pop over.
This is the code for the method on the View Controller that is displayed inside the popover:
- (void) exitView: (id)sender {
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.detailViewController exitDrill];
}
Then the simple method to dismiss on the Detail View Controller:
- (void) exitDrill {
if(dtController != nil){
[dtController dismissPopoverAnimated: YES];
[dtController release];
}
}
I like the ability to do this because it give me a way to show a user how they can exit a pop over. This may not be necessary in future versions of the app; for right now, while this paradigm is still new to the platform, I prefer to let the users gexit a display in a couple fo different ways to make sure I minimize frustration.
As Ed Marty already wrote
If you want to dismiss a popover by pushing a button, some place relevant should keep a reference to the popover
This is very true; however, when showing a UIPopoverController, the class opening the popovercontroller keeps this resource already. So, what you could do is to use this class as the delegate class for your Popover Controller.
To do so, you could do the following, which I use in my code.
In the class opening the popover, this is my code:
- (void)showInformationForView:(Booking*)booking frame:(CGRect)rect
{
BookingDetailsViewController *bookingView = [[BookingDetailsViewController alloc] initWithStyle:UITableViewStyleGrouped booking:booking];
[bookingView setDelegate:self];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:bookingView];
self.popController = [[UIPopoverController alloc] initWithContentViewController:navController];
[self.popController setDelegate:self];
[self.popController setPopoverContentSize:CGSizeMake(320, 320)];
rect.size.width = 0;
[self.popController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
- (void)dismissPopoverAnimated:(BOOL)animated
{
[self.popController dismissPopoverAnimated:animated];
}
So what I am doing here is creating a UINavigationController and setting a BookingDetailsViewController as its rootViewController. Then I am also adding the current class as delegate to this BookingDetailsViewController.
The second thing I added is a dismissal method called dismissPopoverAnimated:animated.
In my BookingDetailsViewController.h I added the following code:
[...]
#property (nonatomic, strong) id delegate;
[...]
And in my BookingDetailsViewController.m I added this code:
[...]
#synthesize delegate = _delegate;
- (void)viewDidLoad
{
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(closeView)];
[self.navigationItem setRightBarButtonItem:closeButton];
[super viewDidLoad];
}
- (void)closeView
{
if ([self.delegate respondsToSelector:#selector(dismissPopoverAnimated:)]) {
[self.delegate dismissPopoverAnimated:YES];
}
else {
NSLog(#"Cannot close the view, nu such dismiss method");
}
}
[...]
What happens is that when the "Close" button in the UINavigationController is pressed, the method closeView is called. This method check if the delegate responds to dismissPopoverAnimated:animated and if so, it calls it. If it does not respond to this method it will show a log message and do nothing more (so it wont crash).
I have written my code using ARC, hence there is no memory management.
I hope this helped you.