PopOver and Tableviews - cocoa-touch

Quick question on popovers, i seem not grasp a way of closing a popview when i select something from it (tableview)
so i have a list items on a tableview which popup using a UIPopoverController so when i select an item i'd like to the popove to fade away.
MainViewController
- (IBAction)popoverFontName:(id)sender
CGRect popoverRect = [self.view convertRect:[popoverFontName frame]
fromView:[popoverFontName superview]];
TitleController *titleC=[[TitleController alloc]init];
popup =[[UIPopoverController alloc]initWithContentViewController:titleC];
[popup presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popup setPopoverContentSize:CGSizeMake(50.0, 300.0)];
[titleC release];
}
TitleController
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedLang = [titleList objectAtIndex:indexPath.row];
//Initialize the detail view controller and display it.
MyDetViewCont *myDetViewCont = [[MyDetViewCont alloc] initWithNibName:#"myDetViewCont" bundle:[NSBundle mainBundle]]; // view controller instance
}
On the title contoller i dont know how to dismiss the popover

You can call dismissPopoverAnimated: on the popoverController. You should keep an instance of your popover as an instance variable in order to dismiss from the UITableViewDelegate methods.

Related

data from plist is loading detail view in a dark screen

So i have set a plist and everytime someone click on the cell in table view i want it to load in the detail view. The only problem is that when it is clicked it loads a dark black screen with nothing on it. Any suggestions?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Index Selected,%d",indexPath.row);
WebViewController *modalView = [[WebViewController alloc] init];
NSString *urltoPass = [NSString stringWithString:[[tableData objectAtIndex:indexPath.row]objectForKey:#"cellSubtitle"]];
modalView.urlString = [[NSString alloc] initWithFormat:#"http://%#",urltoPass];
modalView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:modalView animated:YES completion:nil];
}
You forgot to actually present the new view controller. You have to add the following line to the bottom of your didSelectRowAtIndexPath: method.
[self presentViewController:modalView animated:YES completion:nil];
Also, you can remove the line below entirely as it doesn't do anything in its current form.
[self.navigationController modalTransitionStyle];

Edit Item in Bottom Toolbar Xcode and Objective C

I have a Navigation Controller. I want to put the edit button on the bottom in the toolbar instead of in the top navigation bar.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
The problem I have is that when I add the edit button to the Bottom Toolbar. like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.christmasGifts removeGiftAtIndexPath:indexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
//Do not let the user add if the app is in edit mode.
if(editing)
self.navigationItem.rightBarButtonItem.enabled = YES;
else
self.navigationItem.rightBarButtonItem.enabled = YES;
}
And then link the edit button setEditing: in the toolbar it does not show a done button like the Edit button in the top navigation. it just stays the same.
But you can delete an item, but you cannot reset the state to normal again with the bottom button.
I need to be able to go back to the previous controller from this Navigation Controller. but the edit button hides the back button.
Edit.
I know I can by code add the toolbar.
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(setEditing:)];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:70/255.0f green:155/255.0f blue:19/255.0f alpha:1.0]];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:editButton,anotherButton, nil];
self.toolbarItems = arrBtns;
and
[self.navigationController setToolbarHidden:NO];
or even
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 500, 400, 40)];
[self.tableView addSubview:toolBar];
In your ViewController.h, declare (and connect them in XIB)
#property (strong, nonatomic) IBOutlet UIToolbar *toolbar;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *toolbarbutton;
- (IBAction)toolbarbuttonTouched:(id)sender; // Connect with UIBarButtonItem in XIB
In your ViewController.m, paste down the following function.
- (IBAction)toolbarbuttonTouched:(id)sender
{
if ([self.toolbarbutton.title isEqualToString:#"Edit"])
{
[self.tableView setEditing:YES animated:YES];
self.toolbarbutton.title = #"Done";
}
else
{
[self.tableView setEditing:NO animated:YES];
self.toolbarbutton.title = #"Edit";
}
}
And you are all set to go.
Points
Your UIBarButtonItem's initial value for property title must be
set to "Edit"
Code for editingStyle == UITableViewCellEditingStyleDelete should
be working perfectly
EDIT
You don't need the following code:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
//Do not let the user add if the app is in edit mode.
if(editing)
self.navigationItem.rightBarButtonItem.enabled = YES;
else
self.navigationItem.rightBarButtonItem.enabled = YES;
}
Let me know, If it works for you or not.
I was able to do this more simply by using UIViewController's editButtonItem method:
- (void)viewDidLoad {
[super viewDidLoad];
// Place table view Edit button in toolbar, after existing buttons.
NSMutableArray *items = [self.toolbarItems mutableCopy];
[items addObject:self.editButtonItem];
[self setToolbarItems:items animated:YES];
}
This gets the existing array of toolbar buttons (if any) and makes a mutable copy of the array so it can be changed. It then adds UIViewController's built-in Edit button to the toolbar.
The result is a functioning Edit/Done toolbar button that toggles Edit mode on and off, without having to write your own action method to handle that.

detailview screen can not open with pushViewController in a tableview

I have a tabbar in my project and each tab bar has a tableview.
I want to open a detail screen when click a row in a tableview as follows. But nothing happens.
Where's the mistake I'm doing. How should I set up a logic.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailScreen *detail = [[DetailScreen alloc] initWithNibName:#"DetailScreen" bundle:nil];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
There are many possible reasons nothing is happening when you click on a cell.
Put a break point in this method. Is It even being called?
After the App has stopped at the breakpoint, go to the console type po detail after the instance is initialised. Make sure it's not (null)
Also try typing po [self navigationController] to check whether the navigation controller exists.
You've probably not got a navigation controller. How are you creating the tabbarcontroller? in interface builder or through code in the AppDelegate's didFinishLaunchingWithOptions: method?
Have you done this?
YourViewController *yourVC = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourVC];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
try this,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailScreen *detail = [[DetailScreen alloc] init];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
I guess you don't have a navigation controller so
[self.navigationController pushViewController:detail animated:YES];
will not work.
use
[self.view addSubview:detail.view]
or
[self presentModalViewController:detail animated:YES];
instead
If your project is UITabBarViewController based application, i dont think your can navigate to another view using pushViewController unless you have navigationController in your project.
Add your view controller(your table view containing controller) into navigation controller and then start using
DetailScreen *detail = [[DetailScreen alloc] initWithNibName:#"DetailScreen" bundle:nil];
[self.navigationController pushViewController:detail animated:YES];
[self.navigationController pushViewController:detail animated:YES];
Instead move your detail push code to another method and call:
[self performSelector:#selector(myMethod:) withObject:nil]

Tablecell click short delay

When I click on table cell, there's a short delay of 1-2 second before it loads the next view. I've seen some apps that show an activity indicator during that time and that's what I'd like to do. I've added one like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner.frame = CGRectMake(200,200,200,200);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:#"VenueViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vviewcontroller animated:YES];
[vviewcontroller release];
vviewcontroller = nil;}
however this also appears with a delay, and just before the next view is showing. It seems the app freezes for 1-2 seconds after clicking on the table cell so it doesn't even show the activity indicator.
I think the secret is that you should call load method using a performSelector method. Another tip is hiding or showing the activity so it won't consume time this operation.
So this could be a pseudocode of that
Inside your ViewController class definition:
IBOutlet UIActivityIndicatorView *spin; // created in view and hidden
In your implementation...
-(void) load{ // your code
VenueViewController *vviewcontroller = [[VenueViewController alloc] initWithNibName:#"VenueViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vviewcontroller animated:YES];
[vviewcontroller release];
vviewcontroller = nil;
spin.hidden=YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
spinner.hidden=NO;
[self performSelector:#selector(load) withObject:nil afterDelay:0];
}
Hope it helps.

SIGABRT Error using NavController in the PopOverController

I have posted similar question previously, but this time I am providing some code for analysis. I am creating PopOverController in my AppDelegate.m file and I am adding a NavigationController variable which I want to pass to PopOverController.m file so that using that I want to push other views. Here is how I am creating the PopOver in AppDelegate.m
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[SecondViewController class]]){
NSInteger index = [[self tabBarController] selectedIndex];
CGRect buttonFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:index+1] frame];
PopOverViewController *popoverContentController = [[PopOverViewController alloc]init];
UINavigationController *navcon = [[UINavigationController alloc]initWithRootViewController:popoverContentController];
popoverContentController.contentSizeForViewInPopover = CGSizeMake(250, 85);
popover = [[UIPopoverController alloc]initWithContentViewController:popoverContentController];
NSLog(#"X:%f Y:%f",buttonFrame.origin.x,buttonFrame.origin.y);
[popover presentPopoverFromRect:buttonFrame inView:self.tabBarController.tabBar permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
And in my PopOverController.m I am trying to use the NavigationController to choose views like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
sendFeedback *sendEmailViewController = [[sendFeedback alloc]initWithNibName:#"sendFeedback" bundle:nil];
downLoad *downloadFilelViewController = [[downLoad alloc]initWithNibName:#"downLoad" bundle:nil];
if (indexPath.row == 0)
[self.navigationController pushViewController:sendEmailViewController animated:YES];
else
[self.navigationController pushViewController:downloadFilelViewController animated:YES];
}
But when I click on my TabBar item for the PopOver I am getting this SIGABRT message :
Terminating app due to uncaught exception 'NSGenericException', reason: 'The content view controller argument must be the root of its associated view controller hierarchy.'
Any reason why this is appearing? Is there somewhere I am making a mistake in my code ?
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:popoverContentController];
you are trying to put a popover controller in a navigation controller. I don't think you want this.
To put navigation in popover, you should do this,
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:myViewController];
popoverContentController = [UIPopoverController initWithContentViewController:navcon]
and init the navigation controller with the viewController you want, like TableViewController or something else.