Why my iOS tableview do not work - ios5

I am learning iOS programming and I had a problem with tableview.
By tapping a cell in tableview, I want my program change to another view,here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
testViewController* controller=[[testViewController alloc] initWithNibName:#"testViewController" bundle:nil];
controller.imageView.image=[UIImage imageNamed:#"gyf.jpg"];
[self.navigationController pushViewController:controller animated:YES];
}
the view controlled by testViewController is super simple. There is only a image view in it.
But after I tap the cell, there was only a blank view on the screen without any image.
In debugging, after controller.imageView.image=[UIImage imageNamed:#"gyf.jpg"];,I found controller.imageview is nil.
Please tell me where is the problem and why this phenomenon happen,thank you very much
I use Xcode 4.3.3 and run the program on iPhone 5.1 simulator

(This answer assumes that your testViewController displays OK and is just not getting the image you want.)
When you create a new controller and push it, it's view hierarchy isn't loaded until it executes viewDidLoad:. You're trying to update its imageView before that happens. Try adding a UIImage property to testViewController, setting that property instead in this code, and then updating the imageView once all the views are valid in the new controller.

Related

didSelectRowAtIndexPath method does not load new View in iOS app code is inside

I have my first and normal view controller with a UITableView in it. The items that need to be displayed in those rows work perfectly.
Now comes the issue, if the user selects a row, I want a new view opened.
This view is called SecondViewController. so I have the .h, .m and .xib file named that way and it is of the UIViewController class.
I am using a CustomCell for the tableview could this be the issue?
I have #imported everything normally, and this is the code I am using in the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
What on earth am I doing wrong? I have been looking at questions here for hours but nothing seems to be working.
Are you sure that self.navigationController is not nil?
Try to log it, From your code your problem is probably that, your self.navigationController is nil.
This class is pushed from another or are you starting the navigation in this class? If you are starting it in this class, this is your issue, you need to call the navigation that you created and not the one that is supposed to be with your viewcontroller.

How to make the detailview and masterView communicate in the splitViewController?

I have a UISplitView with a masterView and a detailView. The masterView is a UITableViewController (inside a UINavigationControler) and the detailView is a UIViewController (also inside a UINavaigationController).
The UISplitView appears just fine with the masterView and an empty detailView. In masterView, I have this method:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![self splitViewController])
DetailViewController*myDetailView = [[DetailViewController alloc] init];
Item *selectedItem = [items objectAtIndex:[indexPath row]];
[myDetailView setItem:selectedItem];
[[self navigationController] pushViewController:myDetailView animated:YES];
}
This works fine for iPhone.
For iPad, the detailView appears in the masterViewController, allbeit correctly, but the detailview remains empty.
How can I make myDetailview appear in the detailView (right side of the splitView)?
I recommend you to use Matt gemmell's MGSplitViewController
He has customized everything in the split view controller, you can get the master at right side using this and its just weird that you said that UISplitViewController is working fine in iPhone
To be clear UISplitViewController is specific for iPad and not for iPhone
So, please have a knowledge of iPad specific controllers like UIPopOverController and UISplitViewController
In Doc's, you can read this in the first line it self
Split view controllers are for use exclusively on iPad devices.
Attempting to create one on other devices results in an exception.

How to call a detail view from a table view within a Storyboard

I have following problem.
I have created a tab based Application with three Views and Viewcontroller.
FirstView(Start screen stuff), SecondView (Detailpage), ThirdView (Table for listing items).
(Connections from storyboard were set automatically).
In the third view a table is integrated and the content is displayed fine.
Now I would like to call the SecondView , when a row in the table is selected.
I also tried to add a forth View , outside the tabBar Controller, to get the Detailview, but this also did not help.
I have tried several tutorials and forum tips, but cannot get it working.
The class is set to the right ViewController, the identifier is set to detail.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detail animated:YES];
detail.label1.text = #"It is working";
}
When clicking on the row, it becomes blue, but nothing happens. No error Message , nothing.
Hope that you can help :-)
OK, I have tried to "optimize" my design.
Still have the tab based Views, but when clicking on a row in the table, a new (not linked in Storyboard) view should appear to display the details of the selected quote.
I have created a view in the storyboard and called it "detailzitat"
I have created a DetailViewController.h/m as UIViewcontroller class
I have set the custom class to DetailViewController
I import the DetailViewController.h in the ThirdViewController.h
I have modified the didSelectRowAtIndexPath method in ThirdViewController.h accordingly.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"detailzitat"];
[self.navigationController pushViewController:detailVC animated:YES];
}
and my DetailViewController.m looks like
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize label1, labeltext;
- (void)viewDidLoad
{
[super viewDidLoad];
labeltext=#"JUHU";
label1.text=labeltext;
}
But again, nothing happens, besides the row gets blue.
I do not understand. If I am using this code in a Non-Storyboard project, it is working.
What am I doing wrong ? Is there any tutorial for this combination within Storyboards ? Have not found one for this approach yet.
Try to learn from the different tutorials on the web, but the biggest problem is, most ones are not for iOS5 and I am not so good to transfer then.
Hope to get some hints :-)
You might want to think about your design. If I am understanding your description correctly, the user will be on the third tab, tap on a row in a table, and then you will be switching them back to the second tab. A navigation controller might be a more natural, less confusing, choice there.
But in any case, something like this will work sometimes:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC = [self.tabBarController.viewControllers objectAtIndex:1];
detailVC.label1.text = #"It is working"; // <- this will not work consistently!
self.tabBarController.selectedViewController = detailVC;
}
The problem with this is that while the user is on that third tab, it's possible that the second tab view controller's view is unloaded (due to memory pressure for example).
It's also possible the user went from tab 1 to tab 3 immediately and therefore the 2nd tab's view isn't even loaded yet at all. (To even test the above code you would have to select tab 2 and then tab 3.)
If the second tab's view hierarchy is not loaded, the label1 property will be nil, and so this will not work. A better strategy would be to create a new #property of type NSString* on the DetailViewController. And set that property instead of trying to set the label1 directly.
Then in your viewWillAppear: for the DetailViewController you can update your labels as needed. At that point of course you can be sure that label1 is loaded and connected to the correct UILabel.
I hope that helps.
I think problem is at self.navigationController. If your view is not inside the navigation controller this will not work. So what you do is create a new navigation controller object there and then use it to show your detail view.

iOS / Xcode 4: View won't load

This is really frustrating as I've tinkered with previous versions of Xcode and have done this before but playing around with a new app now, it's not working for some reason.
I have the app open to a UITableView and want it to load to a detail UIView once I select a cell. However, when I choose a cell in the iPhone simulator, it just highlights the cell blue and doesn't load the view.
Here is the code I'm using the the RootViewController:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WorkoutViewController *workoutViewController;
workoutViewController = [[WorkoutViewController alloc] initWithNibName:#"WorkoutViewController" bundle:nil];
workoutViewController.workoutName = [workouts objectAtIndex:indexPath.row];
[self.navigationController pushViewController:workoutViewController animated:YES];
[workoutViewController release];
workoutViewController = nil;
I have the view linked to the File Owner in the WorkoutViewController.xib file. When I put in a breakpoint at #implementation WorkoutViewController, it does get to that breakpoint but it goes to the #synthesize line and then jumps right back out to [self.navigationController ...etc]; and never returns back to the WorkoutViewController.m file.
I would guess that you didn't set the ViewController to be the TableView's delegate. To check, open your ViewController xib-file and rightclick FilesOwner. Under Referencing Outlet you would usually have both delegate and data source" connected to your TableView. If that is not the case, drag New Referencing Outlet to your TableView.
If I'm wrong and they are all connected, you might want put a breakpoint at the beginning of your didSelectRowAtIndexPath method. Does the debugger stop there, once you select a row?
It might also be worth mentioning that a breakpoint at #implementation usually doesn't make much sense, you would rather want to place it in a method like init. Also, even though you are using Xcode 4 now, this is unlikely to be the cause of your problem, it looks more like an implementation issue.
Hope this helps, if you need further help just let me know!
Doh! Problem resolved. I had forgotten to actually put the RootViewController into a navigation controller on the MainWindow.xib. Appreciate the responses.

iPhone SDK: Pushed view controller does not appear

I want to add a detail view for one of the cells in my UITableView. I've created a new view controller SyncDetailViewController) and a NIB for the detail view. Didn't make many changes in the new class. Just added a label and outlet for it and connected nib with view controller.
Now I've added this code to my view controller (the one with UITableView, not the new one)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
SyncDetailViewController *dvController = [[SyncDetailViewController alloc]
initWithNibName:#"SyncDetailViewController" bundle:nil];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
The problem is that even if I select any row in the UITableView nothing happens. It doesn't produce any errors or warnings. I've added a breakpoint inside this method and it is reached so this code is executed.
Any ideas are much apprieciated.
"Nothing happens" sound so much like "sending a message to nil". Have you, by chance, checked that self.navigationController is not nil? Or that dvController is well initialized?