dismiss UIPopOverController from contentViewController - iphone

How can you dismiss a popover from the contentViewController, so in the example code I have below I would like to dismiss the UIPopOver from the ProfileViewController code. How do I do this? Another post similar to this suggested to use NSNotification, but how do use it?
- (void)profilePop:(UITapGestureRecognizer *)recognizer
{
ProfileViewController * profile = [[ProfileViewController alloc] init];
CGPoint location = [recognizer locationInView:self.table];
NSIndexPath* indexPath = [self.table indexPathForRowAtPoint:location];
ConvoreCell* cell = (ConvoreCell *) [self.table cellForRowAtIndexPath:indexPath];
profile.uid = [[[self.posts objectAtIndex:indexPath.row] creator] mid];
UIPopoverController * profilePop = [[UIPopoverController alloc] initWithContentViewController:profile];
[profilePop setPopoverContentSize:CGSizeMake(350, 180)];
[profilePop presentPopoverFromRect:CGRectMake(0,0, cell.avatar.frame.size.width, cell.avatar.frame.size.height) inView:cell permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}

I would keep a reference to the popover in your profile class.
profile.popOver = profilePop;
then in the area you'de like to dismiss:
[self.popover dismissPopoverAnimated:YES];

I think keeping a reference is the way to go too, but I don't know if its a good idea to retain it, because when you need to release the popover controller it won't dealloc because there is an extra retain.

Related

iOS. addsubview to master-detail app

I have a master-detail application with news. It works. In detail controller i used UIWebView.
I created comments-class with xib and am trying to add it to my detail view.
Comments are UITableViewController.
After that i can see only comments, not detail-text.
I tried to scroll my webview and there is no text. But in detailviewController i did NSLog for my text and saw it. If i set newsTextHeight to 480 i see an empty view.
What am I doing wrong?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat newsTextHeight = 250;
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
CommentsViewController *commentController = [[CommentsViewController alloc] init];
NewsItem *newsItem = [news objectAtIndex:indexPath.row];
detailViewController.description = newsItem.description;
detailViewController.title = newsItem.date;
commentController.news_id = newsItem.news_id;
detailViewController.news_id = [newsItem.news_id intValue];
int views = [newsItem.viewsCnt intValue];
views++;
NSString *strFromInt = [NSString stringWithFormat:#"%d",views];
newsItem.viewsCnt = strFromInt;
[self.tableView reloadData];
//[self.navigationController presentedViewController];
[self.navigationController pushViewController:detailViewController animated:YES];
detailViewController.navigationController.toolbarHidden = NO;
[detailViewController addChildViewController:commentController];
[commentController didMoveToParentViewController:detailViewController];
[detailViewController.view addSubview:commentController.view];
detailViewController.view.frame = CGRectMake(0, 0, detailViewController.view.bounds.size.width, newsTextHeight);
commentController.tableView.contentInset = UIEdgeInsetsMake(detailViewController.view.frame.size.height, 0, 0, 0);
[commentController release];
[detailViewController release];
}
results: http://s18.postimg.org/5cv8ftbbd/detail.png
What Kiattisak is getting at is that the problem can be that your view detailViewController.view.frame is using autolayout. And therefor won't be able to be resized programmatically. (This is if you are not using storyboards)
In InterfaceBuilder, highlight your view:
And untick the Use Autolayout box in the first tab of the attributes inspector.
Hope it helps.

Hide UIPopover after selecting row

I am new to iPad developer,
I am using UIPopover in my application, when i select any row in my popover, my popover is not getting hide it still in the view, it is getting hide when i touch outside anywhere on the screen.
I want to hide popover after user selects any row.
here is my code snippet,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
for(int index=0;index<=indexPath.row;index++)
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
lbl.text=cell.textLabel.text;
}
}
Logic: When i select any row of popover corresponding text will be fetched and gets stored into label.
Any help will be appreciated.
EDIT
UIViewController* popoverContent = [[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 500, 4)];
popoverPolicyNameTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 250, 200) style:UITableViewStylePlain];// size of tableview inside popover
[popoverPolicyNameTable setDelegate:(id<UITableViewDelegate>)self];
[popoverPolicyNameTable setDataSource:(id<UITableViewDataSource>)self];
[self.view addSubview:popoverPolicyNameTable];
[popoverPolicyNameTable release];
[popoverView addSubview:popoverPolicyNameTable];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 200); //size of popover border
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(350,100, 35, 35) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //size of arrow
[popoverContent release];
[popoverView release];
You need to implement something like this
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController1{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
Add one variable inside the Class which is having your UITableView:
id parent;
Then properties and synthesize it:
#property(nonatomic, retain) id parent;
#synthesize parent;
Then where you are creating the object of this ViewController to add in Popovercontroller, do like this
myTableViewController.parent = myPopoverControllerObject;
Now in didSelectRowAtIndexPath:
[parent dismissPopoverAnimated:YES];
And you are done!!!
Use delegation and notify your delegate that the user selected an item in the list. The delegate will know how to dismiss the view controller.
This approach is better because it's container agnostic, and you can reuse your view controller outside a popover: for example, using navigation controller on iPhone
Make Popover controller as class member variable.
Then on clicking on table view click method call dismiss method of that popovercontoller.

xib not connected to controller?

I am extremely new to iPhone development, so please forgive this post if it seems naive. I am trying to simply add a subview to my current view (if this is the best way to open a new screen on iPhone). Here is my code:
QuickCalcController *aViewController = [[QuickCalcController alloc]
initWithNibName:#"QuickCalcController" bundle:nil];
aViewController.view.frame = CGRectMake(0, 100, quickCalcController.view.frame.size.width, quickCalcController.view.frame.size.height);
[self.view addSubview: quickCalcController.view];
self.view.bounds = quickCalcController.view.bounds;
The problem is, when this code gets called, the view shown is not QuickCalcController.xib. It is MainView.xib. The file's owner is QuickCalcController... am I missing something?
Thanks in advance.
You should just ask the mainViewController to present the QuickCalcController:
QuickCalcController *aViewController = [[QuickCalcController alloc]
initWithNibName:#"QuickCalcController" bundle:nil];
[self presentModalViewController:aViewController animated:YES];
[aViewController release], aViewController = nil;
Then to dimiss QuickCalcController just call [self dismissModalViewControllerAnimated:YES]; in QuickCalcController.

How to use UIPopoverController with fromRect

Could anyone please tell me what the rect and view should be? I do not understand what I shall pass to the selector. An example would be great!
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPopoverController_class/Reference/Reference.html
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
I use this pretty often. Let's say you want to tap on an image and present a popover with information about it. Assuming you have a gesture recognizer with the selector method (handleImageTap:) on your image, here would be an example code to make that happen:
- (void)handleImageTap:(UIGestureRecognizer *)gesture {
// initialize your popover view controller and assign it to your popoverController
MyPopoverViewController *content = [[MyPopoverViewController alloc] init];
popoverController = [[UIPopoverController alloc] initWithContentViewController:content];
popoverController.popoverContentSize = CGSizeMake(600, 600);
popoverController.delegate = self;
[content release];
if (popoverController.popoverVisible == NO) {
// you can find the tappedImage through the gesture by searching up superviews if you don't already have a reference to it;
[popoverController presentPopoverFromRect:[tappedImage frame] inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[popoverController dismissPopoverAnimated:YES];
}
}
So basically, view will be self.view becuase you are displaying it from the current view controller. The rect is just whatever rect you want the popover to display from. In this case, it is set up to be displayed from the frame of an image. I hope this helps you. If something is still confusing, I'll be happy to try and clear it up

TableView help pushing a custom view

So I am trying to simple traverse to the next level of a table view by doing this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
FiltersController *aFiltersCont = [[FiltersController alloc] init];
aFiltersCont.displayedFilters = [appDelegate.groupedBusiness allKeys];
aFiltersCont.currentLevel = self.currentLevel + 1;
[self.navigationController pushViewController:self animated:YES];
}
}
is there any reason why this would not be pushing the controller? I had a similar problem before, but solved it by displaying the view modally. However, this time, this is in a popover and needs to slide to the next screen inside that popover. Any Ideas? Thanks in advance.
OK I am going to put some more source up here to try and help...
Inside the main view controller I have this code to make the popover from a button:
// Create and configure the filters controller.
FiltersController *aFiltersController = [[FiltersController alloc] initWithStyle:UITableViewStylePlain];
self.filtersController = aFiltersController;
filtersController.appDelegate = self.appDelegate;
UINavigationController *filtersNavController = [[UINavigationController alloc] initWithRootViewController:filtersController];
UIPopoverController *filtersPopover = [[UIPopoverController alloc] initWithContentViewController:filtersNavController];
self.filtersPopoverController = filtersPopover;
filtersPopoverController.delegate = self;
and then I have the code I first posted in my filtersController class. Does that help at all?
[self.navigationController pushViewController:self animated:YES];
Should be
[self.navigationController pushViewController:aFiltersCont animated:YES];
If you are just displaying your sublcass of UITableViewController inside a UIPopoverController, the UINavigationController will not be created for you automatically. You may need to modify the code where you are creating the UIPopoverController with something like this:
MyTableViewController *table = [[[MYTableViewController alloc] init] autorelease];
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:table] autorelease];
myPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
Then you should be able to push and pop navigation controllers on the stack no problem.
you are pushing self which is a reference to the current view controller. you should change the line with the push to the following if aFiltersCont is the viewController you are trying to drill to.
[self.navigationController pushViewController:aFiltersCont animated:YES];
You might be confused about the index. The first index is 0 not 1, so maybe you want indexPath.row == 0?
Also, you should release aFiltersCont before returning (you're leaking a FiltersController and anything it owns every time you run this method.
So maybe this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FiltersController *aFiltersCont = [[[FiltersController alloc] init] autorelease];
aFiltersCont.displayedFilters = [appDelegate.groupedBusiness allKeys];
aFiltersCont.currentLevel = self.currentLevel + 1;
[self.navigationController pushViewController:aFiltersCont animated:YES];
}
}