Error on opening popviewcontroller in iOS 7 - iphone

It looks a simple thing to me but I am not able to understand what mistake I am making. I have to open a popover on click of row in my iPad application. I have done following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
popViewController *vc = [[popViewController alloc] initWithNibName:#"popViewController" bundle:nil];
vc.preferredContentSize = CGSizeMake(500,500);
vc.view.frame = CGRectMake(0, 0, 500, 500);
UIPopoverController *healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
The app crashes when last line is executed. I have searched lots of pages on the web, but I am not able to find its cause. I am not getting any specific error only the Thread 1 error in the main file.
I am using iOS 7.

Try to add your healthPopOverit a member of your class since UIPopoverControllers must be held in an instance variable.
In your .m define it as a property:
UIPopoverController *healthPopOver;
and change:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
popViewController *vc = [[popViewController alloc] initWithNibName:#"popViewController" bundle:nil];
vc.preferredContentSize = CGSizeMake(500,500);
vc.view.frame = CGRectMake(0, 0, 500, 500);
self.healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Related

iOS UITableViewController didSelectRowAtIndexPath not loading another UITableViewController

I have a UITableViewController working just fine with all the content when I tap on one of the cells didSelectRowAtIndexPath is being call but doesn't go to the next UITableViewController. Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewHeadersController *detailViewController = [[ViewHeadersController alloc] initWithNibName:#"ViewHeadersController" bundle:nil];
NSArray *resultsData = [_response objectForKey:#"results"];
detailViewController.urlHeaders = [[resultsData objectAtIndex:indexPath.row] objectForKey:#"url"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
I know didSelectRowAtIndexPath is responding because I see the output of the NSLog.
Any of you knows what is going on or why the uitablevicontroller is not switching to the next UITableViewController?
I'll really appreciate your help
I change the line where is navegationController:
ViewHeadersController *detailViewController = [[ViewHeadersController alloc] initWithNibName:#"ViewHeadersController" bundle:nil];
detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
NSArray *resultsData = [_response objectForKey:#"results"];
detailViewController.urlHeaders = [[resultsData objectAtIndex:indexPath.row] objectForKey:#"url"];
[self presentViewController:detailViewController animated:YES completion:nil];

UITableView Crashes when scrolling

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning num sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning num rows");
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Trying to return cell");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = #"Hello";
NSLog(#"Returning cell");
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected row");
}
- (void)viewDidLoad
{
[super viewDidLoad];
m_titleTable = [[UITableView alloc] init] ;
m_titleTable.dataSource = self;
m_titleTable.delegate = self;
m_titleTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:m_titleTable];
// Do any additional setup after loading the view from its nib.
}
-(void)titleAction:(id)sender{
NSLog(#"Calling");
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self->popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self->popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
The tableview Crash error message
[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0
2012-08-28 14:17:10.539 Demo[2790:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0'
Your problem is that your view controller is being deallocated. You shouldn't be doing something like this:
UIViewController* popoverContent = [[UIViewController alloc]
init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;
Assuming you are using ARC, your popoverView object will be deallocated. Also this is just incorrect. You should almost never instantiate a UIViewController, and definitely never assign one view controller's view instance to another ones!
Here's how I'd rewrite your titleAction: method:
TitleViewController *titleViewController = [[TitleViewController alloc]initWithNibName:#"TitleViewController" bundle:nil];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:titleViewController];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:btn_title.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
The UIPopoverController, at least, should keep a strong reference to the titleViewController and prevent it from being deallocated.
PS I changed your "self->popoverController" to self.popoverController as the -> is not really correct for what you are trying to do. Dot notation makes explicit that you are setting a property on an object.
in viewDidLoad you allocates and initiates a UITableView and connect it to self. You don't have a UITableView? defined and connect in the nib? Without seeing your whole project my guess is that you have UITableViews - this is a problem but not necessarily the crash problem. Did you inherit from UITableViewController as base class?

Moving to second viewController after selecting an item in the rootController

I created app that have tableView with a viewController, what it should do is after selecting an item move to the second viewController and open it with passed data. I am implementing the next thing in the RootTableView:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selecteditem = [listOfItems objectAtIndex:indexPath.row];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
this code should work basicly but after I am selecting an item its been selected(blue colored) and that it, its stays with the selected blue color and nothing happened...after i selecting another row the same thing happens, what's wrong with my code?
You simply need to disable the selection styling when adding cells to your table:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
just put following code into your code
[self.tableView deselectRowAtIndexPath:
[self.tableView indexPathForSelectedRow] animated:YES];
like i.e
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selecteditem = [listOfItems objectAtIndex:indexPath.row];
// implement my code
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedItem = selectedItem;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
I think you should check the navigationController. It might be not initialized.
You must have a UINavigationController to push any controller inside.

Activity Indicators when pushing next view - didSelectRowAtIndexPath

I can successfully push only next view in my iPhone app. However, cause the next view retrieves data to populate the UITableViews, sometimes waiting time could be a few seconds or slightly longer depending on connection.
During this time, the user may think the app has frozen etc. So, to counter this problem, I think implementing UIActivityIndicators would be a good way to let the user know that the app is working.
Can someone tell me where I could implement this?
Thanks.
pushDetailView Method
- (void)pushDetailView {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];
//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
}
//set the ID and call JSON in the controller.
[controller setID:[cell getID]];
//show the view.
[self.navigationController pushViewController:controller animated:YES];
You can implement this in didSelectRowAtIndexPath: method itself.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
This will show the activity indicator at the right side of the cell which will make the user feel that something is loading.
Edit: If you set the accessoryView and write the loading code in the same method, the UI will get updated only when all the operations over. A workaround for this is to just set the activityIndicator in the didSelectRowAtIndexPath: method and call the view controller pushing code with some delay.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
[self performSelector:#selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}
- (void)pushDetailView:(UITableView *)tableView {
// Push the detail view here
}

how display view when cells select in a tableview

i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method