I have this code to present a popover:
- (UIPopoverController *)favoritesPopover
{
if (!favoritesPopover)
{
FavoritesViewController *fvc = [[FavoritesViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fvc];
[fvc release];
favoritesPopover = [[UIPopoverController alloc] initWithContentViewController:navController];
[navController release];
favoritesPopover.delegate = self;
}
return favoritesPopover;
}
- (IBAction)toggleFavorites:(id)sender
{
if (!self.favoritesPopover.popoverVisible)
[self.favoritesPopoverpresentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
else
[self.favoritesPopover dismissPopoverAnimated:YES];
}
Now when didSelectRow is called in FavoritesViewController, I want the popover to be dismissed. I tried [self dismissPopoverAnimated: YES]; but that isn't recognized.
You need to get a reference to the popoverController in your FavoritesViewController. Setup a popoverController property in FavoritesViewController and then you should be able to dismiss it in the didSelectRow method.
Related
I have a simple 3 page app, I can go from page 1 to page 2 swiping left.
However, when I am on page 2, I need to add the ability to go to either page 1 swiping right, or page 3 swiping left, but it just keeps crashing.
I suspect it may be a memory release issue, but I wonder if you could be so kind as to check the code below for any glaring errors?
Many thanks,
- (void)viewDidLoad {
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:swipeLeftRight];
[UISwipeGestureRecognizer release];
}
- (IBAction)swipeLeftDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}
}
- (IBAction)swipeRightDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
EDIT:
You need to implement a method called handleGesture: since that's the action you're passing in to the gesture recognizer.
Fixed, I found an older post by a person called rptwsthi, I used some of that to fix it;
Changed the viewDidLoad {
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
And using this to switch the pages;
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}
}
Not sure how neat it is in a true coders view, but it works, sometimes you just gotta go with what works right! :)
I need to access to parentViewController after presentMoalViewController.
This is my method that I call in the firstViewController to view the secondViewController:
- (void)viewData {
SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:#"select_data"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:#selector(saveData)] autorelease];
viewCtrl.navigationItem.rightBarButtonItem = salvaButton;
UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:#"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:#selector(backView)] autorelease];
viewCtrl.navigationItem.leftBarButtonItem = annullaButton;
[self presentModalViewController:navigationController animated:YES];
}
When I click on saveButton, I try to access to parentViewController in this way, but it not work:
- (void) saveData {
FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
It would be much better if you used a delegate/protocol in your second viewController that the first viewController could set itself as. You can find more info here Pass data back to the previous controller or simply doing a search. This design pattern is used virtually everywhere in iOS programming.
The big advantage is that then your second viewController becomes independent of whoever presented it, and can be easily reused. The technical term would be 'decoupling'.
Last time I did this I used notifications:
[[NSNotificationCenter defaultCenter] postNotificationName:#"saveData" object:dataString];
in your parent vc view didload:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(saveDataNow:) name:#"saveData" object:nil];
For navigationController you can declare a property called something like myController, then you have to initiate your navigationController like this:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.myController = self;
UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:#selector(saveData)] autorelease];
viewCtrl.navigationItem.rightBarButtonItem = salvaButton;
UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:#"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:#selector(backView)] autorelease];
viewCtrl.navigationItem.leftBarButtonItem = annullaButton;
[self presentModalViewController:navigationController animated:YES];
In your parentViewController Class you declare your Method
- (void) saveData {
FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
In your navigationController you can then call [myController saveData];
This should work, if you have any questions, feel free to ask!
You can access your parent view controller using following-
- (void) saveData {
NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
FirstViewController *parentView;
for(int i = 0, i <[activeControllerArray count], i++) {
if([[activeControllerArray objectAtIndex:i] isKindOfClass:[FirstViewController class]) {
parentView= [activeViewController objectAtIndex:i];
break;
}
}
parentView.dataString = [[NSMutableString alloc] initWithFormat:#"new string"];
[parentView performSelectorInBackground:#selector(myMethod) withObject:nil];
[self dismissModalViewControllerAnimated:YES];
}
When click button, a popoverView embedded in NavigationView appear.
My code is as follows:
-(IBAction)myStuffPOP:(id)sender
{
if(myStuffListViewController ==nil)
{
myStuffListViewController = [[MyStuffListViewController alloc] init];
}
UINavigationController *navcontroller=[[[UINavigationController alloc] initWithRootViewController:myStuffListViewController] autorelease];
// Here we create popover controller.
mystuffPopoverView = [[UIPopoverController alloc] initWithContentViewController:navcontroller] ;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
popoverRect.size.height = 40;
[mystuffPopoverView presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[navcontroller setNavigationBarHidden:NO];
}
In NavigationView of PopoverView :
1st subView ---> 2nd SubView ---> 3rd Subview
If I touch 3rd SubView, PopoverView disappears.
My Question is :
When I click button again, I wanna to see lastest View - 3rd SubView not 1st SubView.
Declare UINavigationController *navcontroller; in .h file
Create only onсe navcontroller with your root view controller,
in viewDidLoad method, for example.
- (void)viewDidLoad {
myStuffListViewController = [[MyStuffListViewController alloc] init];
navcontroller=[[UINavigationController alloc] initWithRootViewController:myStuffListViewController]; }
Show UIPopoverController with exist navigation stack
-(IBAction)myStuffPOP:(id)sender {
//Here we create popover controller.
mystuffPopoverView = [[UIPopoverController alloc] initWithContentViewController:navcontroller] ;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
popoverRect.size.height = 40;
[mystuffPopoverView presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
[navcontroller setNavigationBarHidden:NO]; }
Don't forget to release objects
- (void)dealloc {
[myStuffListViewController release];
[navcontroller release];
}
No need to create UINavigationController every time. Save it the same way as MyStuffListViewController
if ( myNavigationController == nil )
myNavigationController = [[[UINavigationController alloc] initWithRootViewController:myStuffListViewController] autorelease];
I'm using presentModalViewController to go from view controller to view controller. Right now the animations are up and down when it goes from one view to another. Can I change it to right and left? I did not see any settings for this in the method presentModalViewController
Ted
Try this before presenting the modal vc:
[sampleViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// or..
// [sampleViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
// or..
// [sampleViewController setModalTransitionStyle:UIModalTransitionStylePartialCurl];
See the modalTransitionStyle Property of a UIViewController - Here are the Reference docs
You can make convenience methods for all styles like so (from Modal View Controller Example):
- (IBAction)showDefault:(id)sender {
SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];
[self presentModalViewController:sampleView animated:YES];
}
- (IBAction)showFlip:(id)sender {
SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:sampleView animated:YES];
}
- (IBAction)showDissolve:(id)sender {
SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}
- (IBAction)showCurl:(id)sender {
SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
I am displaying a UITableViewController inside of a UITabBarController that is being presented modally:
-(IBAction)arButtonClicked:(id)sender{
//this is a uitableviewcontroller
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
arTabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arViewController, lbViewController, nil];
arTabBarController.selectedViewController = arViewController;
[self presentModalViewController:arTabBarController animated:YES];
}
In my viewDidLoad for arViewController method I am setting the navigation items:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
self.title = #"AR";
leaderBoardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(leaderBoardButtonClicked:)];
self.navigationItem.rightBarButtonItem = leaderBoardButton;
}
My navigation bar doesn't appear when it is inside of the UITabBarController, but when I push the view itself I am able to see it.
What am I missing?
Heh, I've been stumped by this too. What you need to do is send the rootViewController.
I've never used a tabBar for anything except on the main screen but ur code will probably look like this:
after arTabBarController.selectedViewController = arViewController;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController: arTabBarController] autorelease];
[self presentModalViewController: navController animated:YES];
Like I said I haven't done it with a tabBar but I'm pretty sure it will be something along these lines
I needed to add a UINavigationBar:
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[self presentModalViewController:arTabBarController animated:YES];
There is a simple solution, put setting in view will appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
hope it help some newbies;