IOS: fill a popover with a tableview - xcode

How can I create a popover and fill it with a tableview? I have a button on a toolbar in bottom of my view, and when i push this button I want to show this popover

Create pushviewcontroller and in the pushviewcontroller you have to give the tableview.
like this.
UIViewController *myPopOver = [[UIViewController alloc] initWithNibName:#"MyPopOverView" bundle:nil];
//(asper your requirement take thiscontroller as tableview)
popoverController = [[[UIPopoverController alloc] initWithContentViewController:myPopOver] retain];

Related

Container view popover view programmatically

In iPad View I add Container View, after adding Xcode automatically create new view and connect to my main view with embed segue. Now in code I try to call this new view programmatically and cannot do this, I need to show this form with popover.
I try:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
[self presentViewController:newPopup animated:YES completion:nil];
This work but show new view in full screen but I need show with popover. I try many things and all not working (.
I try this:
[self performSegueWithIdentifier:#"PaymentCash" sender:nil];
But receive exception SIGABRT
I try this:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popoverController presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Receive exception SIGABRT
Try this:
PaymentCashController *newPopup = [PaymentCashController new];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
[popoverController presentPopoverFromRect:CGRectMake(44, 6, 111, 111) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Same SIGABRT
You need to create UIPopoverController with your view controller and present the popover as shown below.
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] newPopup];
popover.delegate = self;
//set custom size you want for popover
popover.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popover presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Many experiments and now all working:
Remove all segue to my popover View in Storyboard. Do it freeform and change size.
Add to my main View new property
#property (nonatomic,strong) UIPopoverController *popOver;
Set storyboard id to my popover View to "PaymentCash".
In any action on my View to show my popover View add this code:
PaymentCashController *PopoverView = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
self.popOver =[[UIPopoverController alloc] initWithContentViewController:PopoverView];
CGRect popoverStartFrame = {1,500,200,1};
[self.popOver presentPopoverFromRect:popoverStartFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
Only one problem after this, popover View show in no correct place but this another question.

How to use Storyboard to make popover that can be used in code?

I'm building a collection of forms each of which contains several fields. Some of the fields are UITextFields that will display a date. I've created a new class called DatePickerTextField, a descendant of UITextField. When a DatePickerTextField is tapped I'd like for a UIDatePicker control to appear in a popover.
My question is how do I use the storyboard to implement the popover? I can do a segue when there is a specific, visible control in the scene. But how do I represent a generic popover in the scene that I can attach to any instantiated DatePickerTextField that becomes active?
You can create segue that is not connected to any control but I don't think that there would be way to specify anchor point for popover from code. Another option is to create ViewController that is not connected with any segue. When editing storyboard, create ViewController which will be placed in popover, select it and navigate to Utilities pane->Attributes Inspector. Set Size to Freeform, Status Bar to None, specify unique Identifier that will be used to instantiate ViewController from code. Now you can change the size of ViewController by selecting its View and navigating to Utilities pane->Size Inspector.
After that you can create popover from code:
- (IBAction)buttonPressed:(id)sender {
UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
popover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
[popover presentPopoverFromRect:anchor.frame
inView:anchor.superview
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
One caveat is that you need to hold reference to popover as ivar of your class, otherwise it'll crash because UIPopoverController would be released and deallocated after buttonPressed returns:
#interface MyViewController : UIViewController {
// ...
UIPopoverController *popover;
// ...
}
So, I had a similar issue, and in case others might benefit, I figured I'd share it, since I benefit so much from stackoverflow.
This solution allows you to set the anchor of a customizable popover segue. It also allows you to configure the segue to be modal or not (I could not find a way to prevent the segue by dimming the exterior context, so if someone knows how to do that, I would be interested in hearing it); this is accomplished by setting the passthrough views for the popover controller. I also added the capacity to specify a custom view, rather than the view of the source viewcontroller (since I needed this capacity); this portion is not critical to the solution.
DynamicPopoverSegue.h
#interface DynamicPopoverSegue : UIStoryboardPopoverSegue
#property BOOL isModal;
#property UIView* sourceView;
#property CGRect anchor;
#end
DynamicPopoverSegue.m
#implementation DynamicPopoverSegue
- (void)perform
{
if (!self.popoverController.popoverVisible)
{
UIViewController* dst = (UIViewController*)self.destinationViewController;
UIViewController* src = (UIViewController*)self.sourceViewController;
UIView* inView = _sourceView ? _sourceView : src.view;
self.popoverController.contentViewController = dst;
if (!_isModal)
{
[self.popoverController setPassthroughViews:[[NSArray alloc] initWithObjects:inView, nil]];
}
[self.popoverController presentPopoverFromRect:_anchor
inView:inView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
#end
Then you just set your segue to "Custom" in the storyboard, and set the segue class to "DynamicPopoverSegue". In my case, since I wanted to associate it with dynamic layers in a view, I could not set the anchor, so I created the segue by control clicking from the view controller icon in the bar beneath my view controller to the view controller I was using to present the popupover.
To call the popover segue:
[self performSegueWithIdentifier:#"MyPopoverSegue" sender:self];
And to configure the popover segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MyPopoverSegue"])
{
DynamicPopoverSegue* popoverSegue = (DynamicPopoverSegue*)segue;
// set the anchor to wherever you want it to be
popoverSegue.anchor = _destinationFrame;
}
}
- (IBAction)pressItemChooseOprateRoom:(id)sender {
if (isPad){
// UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[self.storyboard instantiateViewControllerWithIdentifier:#"OperateRoomList"];
_myPopover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
CGRect rc=[self getBarItemRc:sender];
[_myPopover presentPopoverFromRect:rc
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[MLControl shared].popover =self;
// [self perfformSegueWithIdentifier:SEGUE_POP_OPERATEROOM sender:self];
}else{
[self iphoneOpenOperateRoomList];
/* [self performSegueWithIdentifier:#"iPhonePushOperateRoom" sender:self];
*/
}
}
-(void)iphoneOpenOperateRoomList{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"OperateRoomList"];
// if (!index.showTabBar) {
// vc.hidesBottomBarWhenPushed = YES;
// }
[self.navigationController pushViewController:vc animated:YES];
}
Just used the answer from Jonnywho for my SWIFT project. In case you need it:
Here's the SWIFT version:
let anchor: UIView = sender
var viewControllerForPopover = self.storyboard?.instantiateViewControllerWithIdentifier("GameAboutViewController") as! UIViewController?
let popover = UIPopoverController(contentViewController: viewControllerForPopover!)
popover.presentPopoverFromRect(anchor.frame, inView: anchor, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
Add a UIView in the scene dock.
You can add it as a subview to any existing view on the view controller.
You can then toggle it's isHidden property as you require.
You can add multiple such subviews and create many such popups.
This technique will save you from setting up a new View Controller and using segues.

How can I open modal view (formsheet) from UITableView?

I am tired try open a modal view controller from ipad application.
How I can show a modal view like formsheet?
here my code block.
like this style opening a window (full screen) but there is don't have any object from my modal view
viewDetail *hd = [[viewDetail alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:hd];
hd.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:nav animated:YES];
[hd release];
[nav release];
just i want open a formSheet modal view when selected a value from UITableView
by the way, i am development on xcode 4.2.1 and usind storyboard features
thanks for help.
like that give a SIGABRT error
viewDetail *hd = [[viewDetail alloc] initWithNibName:#"viewDetail" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:hd];
hd.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:nav animated:YES];
[hd release];
[nav release];
I fixed my solution, worked a xib file out of story board.
But why wasn't it working in storyboard? There is a way for that?
If you're using storyboards, you have to set [self.storyboard instantiateViewControllerWithIdentifier:#"viewDetail"]; as your viewDetail View Controller. (Assuming you set the Storyboard Identifier accordingly)
hope that helps.

IOS: use navigation controller in a subview

In a view of my program I have a button and with this button I open a subview; this subview is a view with a tableview. I want go to another view when I push a row of the tableview so I want to make this with a navigation controller; how can I do this?
Your table view delegate will receive a tableView:didSelectRowAtIndexPath: message when you click on one of the table's rows.
You can put there your code to create the UINavigationController and push on to it your new view.
This sample code (from another answer of mine on S.O.) shows how you can do that:
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];
This should help you getting things on track.
One side note: you might think of having a navigation controller from the very start, this would make your UI more "well-behaved", but this depends ultimately on your app requirements.
If UIViewController addSubView a view of UITableViewController,you want to push in the method of tableView:didSelectRowAtIndexPath,you should check if self.navigationController is nil. If it's nil,You probably should use
[self.parentViewController.navigationController pushViewController:controller animated:YES];
if self.parentViewController is also nil,Sometimes,you have to set a #property to point out the parentViewController In UITableViewController,like:
#property (nonatomic, weak) UIViewController *parentVC;
and in UIViewController:
UITableViewController *tableViewVC = [[UITableViewController alloc] init];
tableViewVC.parentVC = self;
In UITableViewController ,-tableView:didSelectRowAtIndexPath:
[self.parentVC.navigationController pushViewController:controller animated:YES];

How can I add a UIView as a subview of UIViewController when a button clicked?

In my app I need to add a UIView dynamically whenever user taps on a button in my main UIViewController. How can I do this?
Create a new method in your view controller with this signature: -(IBAction) buttonTapped:(id)sender. Save your file. Go to Interface Builder and connect your button to this method (control-click and drag from your button to the view controller [probably your File's owner] and select the -buttonTapped method).
Then implement the method:
-(IBAction) buttonTapped:(id)sender {
// create a new UIView
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,100)];
// do something, e.g. set the background color to red
newView.backgroundColor = [UIColor redColor];
// add the new view as a subview to an existing one (e.g. self.view)
[self.view addSubview:newView];
// release the newView as -addSubview: will retain it
[newView release];
}

Resources