HI
I am working on UITablview based project. i like to load a new view when ever a cell got click. i am using following code in didselectRowAtIndexPath delegate method.
The below coding is not showing any error but new view not get load and [self navigationController] is returning null.
inside Viewdidload function [self navigationcontroller] returning proper value. but inside Delegate method [self navigationcontroller] returns null.
/// inside appDelegate class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.viewController = [[LAMainViewController_iPhone alloc]initWithNibName:#"LAMainViewController_iPhone" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc]initWithRootViewController:viewController];
[window addSubview:controller.view];
[controller release];
[window makeKeyAndVisible];
return YES;
}
/// inside LAmainviewcontroller.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
//self.navigationController.navigationBar.backgroundColor =[UIColor clearColor];// .title=#"test";
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *materialPlistPath = [[NSBundle mainBundle]pathForResource:#"materials" ofType:#"plist"];
materialList = [[NSArray alloc]initWithContentsOfFile:materialPlistPath];
materialTable.backgroundColor = [UIColor blackColor];
NSLog(#" dud kiad navigationController %#", self.navigationController);
//2010-10-20 15:22:03.809 LabAssistant[17368:207] dud kiad navigationController <UINavigationController: 0x5f3b160>
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
NSIndexPath *indexPath = [materialTable indexPathForSelectedRow];
[materialTable deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:#"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease];
materialPropertyListView.chemicalName = [[materialList objectAtIndex:[indexPath row]] objectForKey:#"materialProperty"];
[[self navigationController] pushViewController:materialPropertyListView animated:YES];
NSLog(#"%#",[self navigationController]);
///2010-10-20 16:20:42.634 LabAssistant[17656:207] navigationController (null)
}
please help me to fix this issue.
thanks!
Remove the autorelease from the init statement. Actually the viewcontroller is getting released before it gets pushed.
instead of
LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:#"LAMaterialPropertiesViewController_iPhone" bundle:nil] autorelease];
try this
LAMaterialPropertiesViewController_iPhone *materialPropertyListView = [[LAMaterialPropertiesViewController_iPhone alloc] initWithNibName:#"LAMaterialPropertiesViewController_iPhone" bundle:nil];
Hope this will solve your problem.
I think the table view delegate is called before the navigation controller is set to the view controller.
Can you try reloading the tableview [tableview reloadData]; in viewWillAppear or viewDidAppear?
Related
i implemented a split view in my app. When my app launch it shows fine.
masterview=Leftside view & detailView = Home view
But my master view also contains 2 Table view. when i clicked the any row of table view, the detail view(Now, class view) is not get display.
Mean i want to disply display mutiple detail view as per master view's table row selected.
my code for split view is as follow:
// AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LeftViewController *masterViewController = [[LeftViewController alloc] initWithNibName:#"LeftViewController_iPad" bundle:nil] ;
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController] ;
HomeViewController *detailViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
masterViewController.home_Detail = detailViewController;
self.splitViewController = [[UISplitViewController alloc] init] ;
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers=[NSArray arrayWithObjects:masterNavigationController,detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
}
//LeftView.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.appDelegate.splitViewController viewWillDisappear:YES];
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeAllObjects];
if (tableView==tbl_class)
{
self.class_VC=[[Class_Vice_ViewController alloc]initWithNibName:#"Class_Vice_ViewController" bundle:nil];
[[NSUserDefaults standardUserDefaults]setInteger:[[[classNames objectAtIndex:indexPath.row]valueForKey:#"class_id"]intValue] forKey:#"psel_class"];
NSLog(#"%d",[[[classNames objectAtIndex:indexPath.row]valueForKey:#"class_id"]intValue]);
[viewControllerArray addObject:self.class_VC];
self.appDelegate.splitViewController.delegate = self.class_VC;
[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
[class_VC viewWillAppear:YES];
}
}
Help me to Solve this issue
The offending line is this one in the didSelectRowAtIndexPath: method:
[self.appDelegate.splitViewController viewWillDisappear:YES];
I don't know why you put that in, but if you remove it, it should work. You also don't need this line:
[self.class_VC viewWillAppear:YES];
While it is quite common to override viewWillAppear: and viewWillDisappear:, you almost never call them like you're doing.
I have two table view in one view controller.
They works great! But they are not pushing to any vc.
Under -(void) viewDidLoad method in my main view controller:
horizontalViewController = [[HorizontalViewController alloc] init];
verticalViewController = [[VerticalViewController alloc] init];
[horizontalTableView setDataSource:horizontalViewController];
[verticalTableView setDataSource:verticalViewController];
[horizontalTableView setDelegate:horizontalViewController];
[verticalTableView setDelegate:verticalViewController];
horizontalViewController.view = horizontalViewController.tableView;
verticalViewController.view = verticalViewController.tableView;
What can I do?
Thanks.
refer a following code. If you want use a pushViewController method.
You must be have a NavigationViewController.
so, your structure is a little complex. one ViewController has number of Two TableViewController. one ViewController is not have NavigationController. NavigaitonViewController necessarily belong to the app when it runs because it should be configured.
TwoTableViewsAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *naviController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[window setRootViewController:naviController];
[window makeKeyAndVisible];
return YES;
}
TwoTableViewsViewController.m
- (void)viewDidLoad {
if (firstController == nil) {
firstController = [[FirstTVContoller alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTVController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];
[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
firstController.rootViewController = self;
secondController.rootViewController = self;
[super viewDidLoad];
}
FirstTVContoller.h , SecondTVController.h
#import <Foundation/Foundation.h>
#interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
#property (nonatomic, retain) UIViewController *rootViewController;
#end
FirstTVContoller.m , SecondTVController.m
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VerticalDetailViewController *verticalDetailViewController = [[VerticalDetailViewController alloc] initWithNibName:#"VerticalDetailViewController" bundle:nil];
[[self.rootViewController navigationController] pushViewController:verticalDetailViewController animated:YES];
}
I am having a popovercontroller to dispay a search page ,it shows the search page nicly,when the user tap the cell inside the popover it will show the corresponding page inside the popovercontroller,I dont want it,so I put the NSNotification for displaying the popover,and it works fine,but I got a problem that ,but navigation is not happen there in popovercontroller ,only dismissal happen.
this is my code to create a popover
-(void)revealRightSidebar:(id)sender
{
searchpage* popoverContent = [[searchpage alloc]
init];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:popoverContent] autorelease];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover =
CGSizeMake(320,650);
//create a popover controller
self.popup = [[UIPopoverController alloc]
initWithContentViewController:navigationController];
[self.popup presentPopoverFromRect:_btnsearch.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverContent release];
[self resetReadViewToVerse:1];
}
in viewDidLoad method of this page i put the notification
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissThePopover) name:#"popoverShouldDismiss" object:nil];
}
in searchpage i put this code to navigate to the corresponding search result page
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:#"ParallelReadViewController" bundle:nil];
//detailViewController.firstString = firstString;
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
delegate.selectedBook = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"book"];
delegate.selectedChapter = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"chapter"];
delegate.selectedVerse = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"verse"];
[delegate reloadVerses];
[[NSNotificationCenter defaultCenter] postNotificationName:#"popoverShouldDismiss" object:nil];
}
but when I remove the notification it navigate to the correspondent search in parallelReadViewController page ,but within the popover itself,here the popover dismiss,but no navigation.please help me to do this.
You must be having a navigationController declared inside the AppDelegate class. Use that navigationController for pushing the required view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:#"ParallelReadViewController" bundle:nil];
//detailViewController.firstString = firstString;
// ...
// Pass the selected object to the new view controller.
[delegate.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
delegate.selectedBook = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"book"];
delegate.selectedChapter = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"chapter"];
delegate.selectedVerse = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"verse"];
[delegate reloadVerses];
[[NSNotificationCenter defaultCenter] postNotificationName:#"popoverShouldDismiss" object:nil];
}
Hope this might help you.......
I am having a problem with title and UINavigationController. I am using the following code at UIViewControllerCurrent and when I get back from NextViewController, the title of the NavBar is still "Previous" ..Can anyone kindly help me out with this problem ? Thanks.
[self setTitle:#"Previous"];
NextViewController *controller = [[NextViewController alloc] init];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
- (void)viewWillAppear:(BOOL)animated; {
[self setTitle:#"Real Title"];
[super viewWillAppear:animated]; }
I would try to exchange the two lines
[self setTitle:#"Real Title"];
[super viewWillAppear:animated];
so that you get
[super viewDidAppear:animated];
[self setTitle:#"Real Title"];
and I would put these into viewDidAppear.
Try using UINavigationController delegate methods below.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
Use one of the delegate method to change title.
If dasdoms solution is not working the try the following.
In the NextViewController just before poping out run this code.
for (int i =0; i < [[self.navigationController viewControllers] count]; i++)
{
UIViewController *aController = [[self.navigationController viewControllers] objectAtIndex:i];
if ([aController isKindOfClass:[PreviousViewController class]])
{
PreviousViewController *aPreviousViewController = (PreviousViewController *)aController;
aPreviousViewController.title = #" Real Title";
}
}
I have the following action that gets called when a button gets clicked. The log message gets printed yet, the view doesn't change. This function is in a UIViewController.
Any ideas what I am doing wrong? Why isn't the new view being displayed with a red background?
- (void)viewDidLoad
{
[levelButton2 addTarget:self action:#selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
//MORE CODE
}
- (void) clickButton
{
NSLog(#"Clicked Button");
UIViewController *myViewController = [[UIViewController alloc] init];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];
//[self.navigationController pushViewController:nextController animated:YES];
}
-(void) clickButton
{
NSLog(#"Clicked Button");
MyFirstController *myViewController = [[MyFirstController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];
}
Then after go into the MyFirstController.m file and
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.title = #"My First View";
}
return self;
}
I hope it will help you.
You havent used or mention NIB file in that controller..Add NIB file or else make set root view controller of a navigation with a view controller and push the view controller
You have to change your IBAction method a little bit as,
- (void) clickButton
{
NSLog(#"Clicked Button");
MyFirstController *myViewController = [[MyFirstController alloc] initWithNibName:#"MyFirstController" bundle:nil];
myViewController.title = #"My First View";
myViewController.view.backgroundColor = [UIColor redColor];
//to push the UIView.
[self.navigationController pushViewController:myViewController animated:YES];
//[self.navigationController pushViewController:nextController animated:YES];
}