PopOver from UIButton - iphone

Here's my code
-(IBAction)showMenu:(id)sender
{
Demo *mainMenuTableView = [[Demo alloc] init];
UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:mainMenuTableView];
[pop setDelegate:self];
}
Demo is my xib that contains a tableview controller stuff. This "Demo" works just fine as a fullscreen view.
I'm trying to create a popover with this view, but I've tried what I think is every other solutions on stackoverflow, but I still cannot determine how to create and call the popover...
I'm sure I'm like a line of code or two away... I hope.
Any help would be appreciated!
Thx!

After you create the popover controller, you have to tell it to present the popover. You can use either presentPopoverFromRect:inView:permittedArrowDirections:animated: or presentPopoverFromBarButtonItem:permittedArrowDirections:animated:. For example, I assume that you have connected showMenu: as the action of a UIButton. So you can add this at the end of showMenu::
UIButton *button = (UIButton *)sender;
[pop presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
You also need to put a reference to the popover controller in an instance variable or property. Otherwise the popover controller will be deallocated when showMenu: returns, which will cause a crash. Thanks to Floydian for pointing this out.

You need to retain the "pop" variable! Just set a UIPopoverController "POV" as your property and use below code in your IBAction.
self.POV = [[UIPopoverController alloc]initWithContentViewController:mainMenuTableView];
[self.POV presentPopoverFromRect:button.bounds
inView:button
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];

Related

Switching Views gives me a SIGABRT Message

I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];

How can I hide a button in a different view controller?

I am using this code to hide a button in a different view controller, but the button does not get hidden when the button is pressed to hide the button in the other view controller.
This is the code I am using to hide the button in the other view controller:
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Exited");
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Why is this not working?
Thanks!
take a BOOL variable in ViewController controller and make the property and synthesize also.
and do this.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.check = YES;
in the view controller viewdidload
write this
if(self.check)
[mainbutton1 set hidden:YES];
The other answers should work unless...
Judging by your code I am going to guess that you are trying to hide a button on the viewController that presented the modal view?
If this is correct then what you are doing will not work as you are creating a new instance of ViewController which is not the already existing viewController you want to use.
Although the docs say that it is fine to call [self dismissModalViewControllerAnimated:YES]; from the presented modal view I tend to set up a delegate to handle the dismissal like in Apple's utitliy app template.
The reason this isn't working is because even though you have alloc'd and init'd the ViewController properly, the actual elements of that vc ViewController (including mainbutton1) have not been loaded yet.
Hitman has the right idea (and I'm voting his idea up).
Either put in a BOOL property for setting mainButton1 to hidden when the view appears, or call your [mainButton1 setHidden: YES] right after you explicitly display the view (via animation or adding subviews or whatever).
From your question it sounds like you want to hide the button in an existing view controller, whereas in your code you are creating a new one
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Either the view controller which you observe is not the one you expect or the mainbutton1 outlet is not connected properly. You can check if the memory controller is the one you expect by logging its memory address.
NSLog(#"Hid button for view controller %p", vc);
And doing the same in the viewDidAppear callback of ViewController
NSLog(#"In viewDidAppear for view controller %p", self);
It seems you want a certain button to be hidden if something has been happening somewhere else.
You COULD, somewhat as a hack (but I don't mind that very much) control this with a variable on your AppDelegate for instance.
When the "something" is happening "somewhere else", do this:
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
appDelegate.shouldHideThatOtherButtonLater = YES;
Then, when you create your new ViewController later on you could use this value to determine if your button should be visible or not like this:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
[vc.mainbutton1 setHidden: appDelegate.shouldHideThatOtherButtonLater ];
You will in this case have to prepare your AppDelegate for this by creating and synthesizing that shouldHideThatOtherButtonLater-property.

is there a way NOT to have the popover dismissed when pressing outside it?

I know the SDK documentation says
Taps outside of the popover’s contents automatically dismiss the popover.
But I'm sure the smart people here found a way :)
maybe I should overwrite the popover dismiss function?
Thanks
EDIT:
I tried using the passthroughViews as was suggested here, and it works perfectly. Here's the code for whoever needs it - in this example, I put self.view in the array, which means that where ever outside the button where the popover was originated, nothing dismiss the popover.
popoverController.passthroughViews = [[[NSArray alloc] initWithObjects:self.view, nil] autorelease];
You need to set the passthroughViews property. From the documentation:
An array of views that the user can interact with while the popover is visible.
#property (nonatomic, copy) NSArray *passthroughViews
When a popover is active, interactions with other views are normally disabled until the popover is dismissed. Assigning an array of views to this property allows taps outside of the popover to be handled by the corresponding views.
Set passthroughViews to an array of view(s) that you want to handle the touch event instead of just dismissing the popover.
There is a very simple and legit solution. In the view controller that presents your UIPopoverController, conform to the UIPopoverControllerDelegate protocol and implement the following delegate method. I just tested this and it does prevent popover to dismiss.
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return NO;
}
Just make sure that you have set the delegate of your popover controller to the view controller that implements this.
You can dismiss the popover by using [popoverController dismissPopoverAnimated:NO]; method.
The accepted answer does not really answer the question, "is there a way NOT to have the popover dismissed when pressing outside it?", imo. It does give a possible view but could require hackish access to all parent views and determining what views are on the screen etc. The question could be rephrased as, "how do I make a popover view modal?"
You would do this like so, with a done button to close the popover:
UIViewController* vc = [[[UIViewController alloc] init] autorelease];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done"] style:UIBarButtonItemStyleDone target:self action:#selector(processDoneAction)] autorelease];
[vc.navigationItem setLeftBarButtonItem:doneButton];
vc.modalInPopover = YES;
//If you want full screen:
vc.modalPresentationStyle = UIModalPresentationFullScreen;
vc.wantsFullScreenLayout = YES;
UINavigationController* navC = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
UIView* view = create your view
vc.view = view;
UIPopoverController* pc = [[[UIPopoverController alloc] initWithContentViewController:navC] autorelease];
pc.delegate = self;
self.popoverController = pc;
Then you'll in your processDoneAction method you will need to dismiss the popover. Other considerations would be dismissing and redisplaying on device orientation changes, but I will leave that to another exercise as that has been answered previously on stackoverflow.

How to insert a subview with xib?

I'm confused something fierce over having multiple views. I simply want to have a button on my main view that activates a new view, which in turn would have an (x) button which goes back to main view. For the life of me, I can't figure out how to do this with two separate .xib files. How might this be done?
Thanks!
It might be easiest to just use the utility application as a template.
From there, you can see how you would load view controllers and nibs in order to bring up the new view followed by how you would exit it.
I actually solved it by doing it this way:
NewViewController *new = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
new.delegate = self;
new.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:new animated:YES];
[new release];
You are using a navigation controller, right?
On the IBAction associated with a button tap on the main view, put
NewViewController *newView = ... // [alloc - init your view here if you haven't already]
[self.navigationController pushViewController:newView animated:YES];
And on the button on newView,
[self.navigationController popViewControllerAnimated:YES]

UIView disappears after modalViewController is removed from superview

In my app I have a tabBarController and in it a navigationController. One of my view controllers is a TableViewController and under the navigationBar i added a uiView as a subview to the view like this:
rectangleInfo = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,26)]; rectangleInfo.autoresizingMask = (UIViewAutoresizingFlexibleWidth); rectangleInfo.backgroundColor = [UIColor darkGrayColor]; [self.view addSubview: rectangleInfo];
when I click on a cell in the tableView I push an UIViewController like this:
[feedViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:feedViewController animated:YES];
After i pop the modal view for a couple of times with it from the tableViewNavigationController disappears the rectangleInfo UIView.
I pop my modalview like this:
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
[self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self dismissModalViewControllerAnimated:YES];
any idea why that subview (rectangleInfo) of the tableViewController dissapears after i remove the modal view from the superview?
thank you in advance.
I'm curious as to what you are trying to do with your rectangleInfo view? By the size of it, it looks like you are trying to mimic the status bar. Why? You can just hide the status bar if you want.
Another thing you can try is to create the UIView visually in Interface Builder. Don't actually add it to your main view, but create it as a separate UIView in the XIB with the appropriate size, etc. Then create an outlet for it in Xcode and connect it. Next, add it as a subview in code when your view controller loads. See if that makes a difference. This is especially strange since you say it only disappears after popping it several times. Do you get the same problem if you push and pop the non-modal way, e.g.:
[[self navigationController] pushViewController:feedViewController animated:YES];
[[self navigationController] popViewControllerAnimated:YES];
i solved the problem by implementing correctly the viewDidLoad and viewDidUnload methods for creating and releasing my subviews.