So I have a UINavigationController that pushes a UITableViewController from the rootViewController. From the UITableViewController, I want a UIImagePickerController to be pushed when I click a certain cell. How would I get the navigation controller to display the image picker?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
// don't release imagePicker here
}
Then define UIPickerControllerDelegate in your tableViewController implementation.
Related
I have implemented UIPopoverController with storyboard but i am not able to make it dismiss when I select particular row in UITableView.
When select particular row so that time I want to dismiss the popover but I am not able dismiss it.
I write below code for this:
//Show the popover in Main UIViewController
-(IBAction)clickNotes:(id)sender {
NSLog(#"notes:");
NoteList *objNoteList = [[NoteList alloc] initWithNibName:#"NoteList" bundle:nil];
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.popoverContentSize = CGSizeMake(250, 450);
[popover presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
//Hide the popover in another UIViewController on didSelecteRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = #"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[objNote.popover dismissPopoverAnimated:YES];
}
use
[popover dismissPopoverAnimated:YES];
The following code instantiates a NEW Instance. So it has nothing to do with the already existing popover :Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
Also Instead Of :
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
USE:
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
I.e.: Allocate the instance first and then set its delegate.
Finally replace your method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
with this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
[popover dismissPopoverAnimated:YES];
}
// Create protocol in .h file of controller which contains didSelectRowAtIndexPath method as follows:
#protocol Popoverdelegate <NSObject>
{
-(void)didRowAtIndexPathIsSelected;
}
// Add this property in .h file of the same controller
#property (strong, nonatomic) id<Popoverdelegate> delegate;
// Now implement this protocol in interface which calls popovercontroller
// for ex: #interface ViewController <Popovercontroller>
// then add following properties to viewController .h file
#protocol (strong, nonatomic) UIPopoverController *popoverController;
// Implement popoverdelegate protocol in .m file as
- (void) didRowAtIndexPathIsSelected
{
[self.popoverController dismissPopoverAnimated:YES];
}
// Replace your code as follows
-(IBAction)clickNotes:(id)sender
{
NoteList *objNoteList = [[NoteList alloc] initWithNibName:#"NoteList" bundle:nil];
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
self.popoverController = popover;
self.popoverController.popoverContentSize = CGSizeMake(250, 450);
[self.popoverController presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = #"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[self.delegate dismissPopoverAnimated:YES];
}
The smartest thing to do here (imho) is to follow this example code, I do it every time:
// firstViewController.h
#interface firstViewController : UIViewController <SecondDelegate>
{
SecondViewController *choice;
}
// firstViewController.m
- (void)choicePicked(NSString *)choice
{
NSLog(choice);
[_popover dismissPopoverAnimated:YES]; //(put it here)
_popover = nil; (deallocate the popover)
_choicePicker = nil; (deallocate the SecondViewController instance)
}
// secondViewController.h (the one that will give back the data)
#protocol SecondDelegate <NSObject>
- (void)choicePicked:(NSString *)choice;
#end
#interface secondViewController : UITableViewController
#property (nonatomic, assign) id <SecondDelegate> delegate;
#end
// secondViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selection = [_yourArray objectAtIndex:indexPath.row];
[[self delegate] choicePicked:selection];
}
I have a view(HomeView) in which i open a popOver. The popoVer content View is (ListView) which contains a Table View.
Now, when i select the row, The HomeView will dismissed & open a new View(MapView).
Till now, It works fine. But my all views contain tabbar in xib. I don't have tab bar in ListView.
So, i just open MapView with present Modal View. But from that my navigation is not working.
My code is as following.
HomeView.m
//open popview with ListView
-(IBAction)btnTableMenu_TouchUpInside:(id)sender{
ListView *popUp=[[ListView alloc] initWithNibName:#"ListView" bundle:nil];
popView = [[UIPopoverController alloc]initWithContentViewController:popUp];
popView.delegate =self;
[popView setPopoverContentSize:CGSizeMake(300, 700)];
[popView presentPopoverFromRect:CGRectMake(150,25,20,50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
ListView.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"DismissModal"object:self];
MapViewController *mapVC=[[MapViewController alloc]initWithNibName:#"MapViewController_ipad" bundle:nil];
[self presentModalViewController:mapVC animated:YES];
//[self.navigationController pushViewController:mapVC animated:YES];
}
How can i solve this??
you just do like this way:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"DismissModal"object:self];
MapViewController *mapVC=[[MapViewController alloc]initWithNibName:#"MapViewController_ipad" bundle:nil];
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:mapVC];
navbar.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
[navbar setModalPresentationStyle:UIModalPresentationFormSheet];
[navbar setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:navbar animated:YES];
}
in MapViewController.m file ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *CancleButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = CancleButton;
}
-(void)cancel
{
[self dismissModalViewControllerAnimated:YES];
}
Do you know the difference btw ..
[self presentModalViewController:controller animated:YES];
& [self.navigationController pushViewController:controller animated:YES];
\When you do presentModalViewController:controller you lost the last Navigation stack.You need to create different navigationController for presenting the View. Read this properly you will get Idea .
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Try following these-
ListView.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"DismissModal"object:self userInfo:[NSDictionary dictionaryWithObject:#"map" forKey:#"selectedView"]];
}
HomeView.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissModal:) name:#"DismissModal" object:nil];
- (void) dismissModal:(NSNotification *)notification
{
[self dismissModalViewController:mapVC animated:NO];
if([[notification.userInfo valueForKey:#"selectedView"] isEqualToString:#"map"])
{
MapViewController *mapVC=[[MapViewController alloc]initWithNibName:#"MapViewController_ipad" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mapVC];
[self presentModalViewController:navController animated:YES];
}
}
i have a TabBarController with 3 Views / Tabs...At one Tab i have an UITableView. Now i want if the user clicks on a cell switch to a detailview...i tried it already with this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
[self.navigationController pushViewController:nextController animated:YES];
}
But it doen´s work...Any Ideas?!
You need to embed the table view controller in a navigation controller
If u want to use navigation controller do like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//for home tab.. u want to add navigation controller
YourTableViewController *objviewController = [[[YourTableViewController alloc] initWithNibName:#"YourTableViewController_iPhone" bundle:nil] autorelease];
UINavigationController *navCtrl = [[UINavigationController alloc] initRootViewController:objviewController];
//for tab2...
YourSecondViewController *objYourSecondViewController = [[[YourTableViewController alloc] initWithNibName:#"YourSecondViewController_iPhone" bundle:nil] autorelease];
//for tab3...
YourThirdViewController *objYourThirdViewController = [[[YourThirdViewController alloc] initWithNibName:#"YourThirdViewController_iPhone" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navCtrl,objYourSecondViewController,objYourThirdViewController,nil];
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
But u can add also add like if don't need navigation controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"detailView"];
[self.view addSubView:nextController];
}
I have a HomeViewController.m in which I push SpecificViewController by self.navigationController,
SpecificViewController *specificViewController= [[SpecificViewController alloc] initWithNibName:#"SpecificViewController" bundle:nil];
[self.navigationController pushViewController:specificViewController animated:YES];
And in SpecificViewController.m I insert a subview
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.view insertSubview:firstViewController.view atIndex:0];
And in FirstViewController.m, there is a tableview. The question is how can I push a new ViewController by navigationController, I have try below, but it does not work.
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailTableViewController *detail =
[[DetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I still tried a new UINavigationController,
UINavigationController *NEWnavigationController;
NEWnavigationController=[[UINavigationController alloc] init];
[NEWnavigationController pushViewController:detail animated:YES];
but it still did not work. Could you give me any suggestion?
There are quite a few things wrong with your approach.
First, is SpecificViewController the first view controller you load? If yes, then you do not push this view controller rather you set it as the root view controller in your application delegate as shown below
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[[SpecificViewController alloc] init];
This will ensure that your Navigation controller is setup and the first view controller that appears is the SpecificViewController
Secondly, you dont insert a view controller as a subview. If you are trying to load a second view controller then you push that onto your navigation controller stack. You can do that from SpecificViewController as shown below
FirstViewController *firstViewController =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.navigationController pushViewController:firstViewController animated:YES];
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?