I have a Master-Detail app and I want to load a new view on the click of rows in Master. If I select row-1, the view is different, and on selection of a different row, new view is presented in Detail portion. How can I achieve this thing?
I am doing something like this. The issue is - If I select any of these 2 rows for the first time, the view actually changes. But again when I click on the other row, it does not change.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
TwitterTweetViewController *detailView=(TwitterTweetViewController *)[storyboard instantiateViewControllerWithIdentifier:#"TwitterDetailView"];
self.splitViewController.mainViewController = detailView;
[self.splitViewController.mainViewController viewDidLoad];
[self.splitViewController viewDidLoad];
}
if(indexPath.row == 1)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
TwitterDetailViewController *detailView=(TwitterDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:#"TwitterDetailView"];
self.splitViewController.mainViewController = detailView;
[self.splitViewController.mainViewController viewDidLoad];
[self.splitViewController viewDidLoad];
}
}
What should I do in order to change the view on selecting these rows?
There is a great method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Use it. It is called right after the click has been made on a cell. You can put some check in there. Short example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
SomeViewController *svc = [[SomeViewController alloc]initWithNibName:#"SomeViewController" bundle:nil];
[self.navigationController pushViewController:svc];
} else if (indexPath.row > 0 && indexPath.row < 5) {
SomeOtherViewController *sovc = [[SomeOtherViewController alloc]initWithNibName:#"SomeOtherViewController" bundle:nil];
[self.navigationController pushViewController:sovc];
} else {
AnotherViewController *avc = [[AnotherViewController alloc]initWithNibName:#"AnotherViewController" bundle:nil];
[self.navigationController pushViewController:avc];
}
//some other code if needed
}
Hope it helps
You can create an enum of your view controllers as :-
enum MyViewControllers
{
viewcontroller1
viewcontroller2
viewcontroller3
}
The constatns in enum would correspond to your viewcontrollers for each indexPAth.
and then in the delegate method of UITAbleVIew didSelectRowAtIndexPath: , use switch case :-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
switch(indexPath.row)
{
case 1:
Load view1
break;
case 2:
Load view2
break;
case 3:
Load view3
break; .
.
so on.
}
}
Or in case of different NavigationControllers (as in iPad )you will have to pass data using delegates and protocol.Define the protocol in rootViewController and then set its delegate in the detail(Where you want to push the viewcontroller)
somethinglike :-
#protocol SendSelectedINdexDelegate
{
#required
-(void)sendTheIndex:(NSNumber)number.
#end
in interface
id <SendSelectedINdexDelegate> delegateSend
set its property:-
#property (nonatomic) id delegateSend;
in .m
#synthesize delegateSend
and in didSelectRowAtIndexPath:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[self delegate]sendTheIndex:indexPath.row];
}
At the receiving controller
conform to protocol
and set
rootview.delegateSend=self;
and implement the method of protocol;
-(void)sendTheIndex:(NSNumber)number
{
//nsnumber is your index perform operation based on this.
}
Take care that if you are using ARC,it forbids synth of a property with unspecified ownership attribute( in .m #synthesize delegateSend).
Related
I want to push a view when I click the cell of my tableview.The code is as followed:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == todoTableViewTag) {
NSLog(#"aa");
ViewControlleraaa* testvc = [[ViewControlleraaa alloc] initWithNibName:#"ViewControlleraaa" bundle:nil];
[self.navigationController pushViewController:testvc animated:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
but it does not work at all.
I have
ViewControlleraaa.h ViewControlleraaa.m and ViewControlleraaa.xib
but I did nothing except creating them.
So where is the problem?
Create a 'generic' segue from your table view controller to ViewControlleraaa and use 'performSegueWithIdentifier'. If that approach is not possible to execute then you may want to try 'presentViewController' instead.
Try creating a manual segue from your table view controller to ViewControlleraaa. Then, as the answerer above me said, you can use performSegueWithIdentifier to call the segue. If you should ever need the table view controller to push any data to the ViewControlleraaa, you can do so using the "prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender" method. The destinationViewController property of "segue" is ViewControlleraaa.
Here's a link to a YouTube video, in case you need any additional help: https://www.youtube.com/watch?v=XApYtAlRDVE
Check self.navigationController is not nil then surly it will work fine.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
I want to pass a value to a property from one class to another.
In LAClaimReportViewController, I have a property
#property (strong, nonatomic) LAClaimReport *claimReport;
I want to pass the value to claimReport property (of LAClaimReportViewController)from below code of masterViewController.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([record.submitted isEqualToNumber:#0] && record != _selectedReport)
{
[self.detailViewController.navigationController popToRootViewControllerAnimated:NO];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LAClaimReportViewController *claimReportViewController = [storyboard instantiateViewControllerWithIdentifier:#"ClaimReportView"];
[self.detailViewController.navigationController pushViewController:claimReportViewController animated:YES];
//Question: i want to assign record to claimReport of LAClaimReportViewController. how to do that.
}
}
#try this
[claimReportViewController setclaimReport:selectedReport];
So I'm using this code... "MFSideMenuDemoStoryboard"
https://github.com/mikefrederick/MFSideMenu/tree/master/Demos/MFSideMenuDemoStoryboard/MFSideMenuDemoStoryboard
And I want to give a name to each cell in the "SideMenuViewController.m" and make each one of these cells show a specific UIView in the "DemoViewController.m". any help with that ?
any help is appreciated, thank you!!
In SideMenuController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DemoViewController *demoController = [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
demoController.title = [NSString stringWithFormat:#"Demo #%d-%d", indexPath.section, indexPath.row];
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
NSArray *controllers = [NSArray arrayWithObject:demoController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
Replace this code with something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//EDIT #1
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
DemoViewController *currentCenterView = [[navigationController viewControllers] firstObject];
[currentCenterView addAnotherView:indexPath.row]; //This is where you will tell your 'middle view' to load another UIView ... You can use the indexPath.row to determine which option was tapped and act accordingly.
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
You obviously need to add a method - (void) addAnotherView:(NSInteger ) row; to your DemoViewController or whatever you've called it.
Edit #1 this will reference the current loaded view. This will only work if you are only using 1 view controller on the navigation controller stack. Otherwise grabbing the first object won't work and you'll need to search for the right index.
In my DetailViewController.h I've declared the property * type as seen below:
#interface DetailViewController : UIViewController
#property (strong, nonatomic) NSString *type;
#end
Then I got a ListViewController which is a UIViewController containing a TableView.
The ListViewController.h looks like this:
#interface ListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#end
In my ListViewController.m I want to change the value of * type a soon I switch the View (view switching is working fine).
I'm doing this by adding the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UINavigationController * navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
DetailViewController * detailController = [[DetailViewController alloc] init];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:navigationController animated:YES];
}
When I put a breakpoint at the second line in this block/function I get detailController with in it * type.
When I NSLog this a few lines later it return "(null)".
The View is changed but but the type is not set.
I can't figure out what I'm doing wrong?
Someone who has a solution for this or can point me to something we're this is explained?
Because I tried searching for the answer but I couldn't find anything equivalent to my problem, or I used the wrong search terms..
in your didSelectRowAtIndexPath: method change the following lines`
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];;
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController: detailController animated:YES];
}`
in your storyboard select DetailViewController and give storyboard id as "DetailController". Make sure your ListViewController is embed in UINavigationController, you can do that by selecting ListViewController and go to editor->Embed in->Navigation Controller. Follow these steps and your problem will be solved.
OR you can give segue from your ListViewController to DetailViewController and use
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //create tableview outlet
[segue.destinationViewController setType:#"video"];
}
What is this for?
UINavigationController * navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
Why are you pushing a navigation controller inside a navigation controller?
[self.navigationController pushViewController:navigationController animated:YES];
detailController was not used or displayed.
Try this instead:
[self.navigationController pushViewController:detailController animated:YES];
Look at the last line - you are telling the nav controller to push itself!
It should be like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailController"];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:detailController animated:YES];
}
Is the navigation controller and the alloc-init method both instantiates the save view controller??
If yes, try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * detailController = [[DetailViewController alloc] init];
detailController.type = #"video";
NSLog(#"Type: %#", detailController.type);
[self.navigationController pushViewController:detailController animated:YES];
}
I have started working with story board ,a new feature in iOS 5,so I started with a tabbed application,then I added a table view controller and filled the cells with some dummy data,now,the table has 4 cells,now on click of each cell,I want to open a newViewController,which will be diffrent for each cell,
So previously,I used to code this way
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0)
{
FirstViewController *settingsView =[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[self.navigationController pushViewController:settingsView animated:YES];
}
if(indexPath.row == 1)
{
SecondViewController *settingsView =[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:settingsView animated:YES];
}
if(indexPath.row == 2)
{
ThirdViewController *settingsView =[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:settingsView animated:YES];
}
}
But know,how shall I do it..please help me out
Thanks & Regards
Ranjit
Maybe this tutorial would help you?
http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-uitableview-tutorial.html
As I see this (performSegueWithIdentifier:sender:) should be the method to perform a new viewcontroller from the storyboard
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/performSegueWithIdentifier:sender: