I'm trying to pass a NSDictionaryfrom a TableViewControllerto a ViewController. In my TableViewController.m. I have this code to navigate to the ViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];
NSLog(#"Info: %#", objectDict);
// Pass object to new page
//UIViewController * vc = [[UIViewController alloc] init];
//[self presentViewController:vc animated:YES completion:nil];
SeeInfoVC *controller = [[SeeInfoVC alloc] init];
controller.data = objectDict;
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"SeeInfoVC"];
[self presentViewController:vc animated:YES completion:nil];
In my ViewController.h I have:
#interface SeeCardVC : UIViewController
{
NSDictionary *data;
}
#property (nonatomic, retain)NSDictionary *data;
#end
And then I'm trying to log data in ViewController.m:
#implementation SeeCardVC
#synthesize data;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"Info: %#", data);
}
But it only gives me null :(
What am I doing wrong?
Let's see what your code is doing here:
// Create new controller, assign objectDict to data property
SeeInfoVC *controller = [[SeeInfoVC alloc] init];
controller.data = objectDict;
//Get Storyboard name
NSString * storyboardName = #"Main";
//Get Storyboard (BTW, you can do this with self.storyboard)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
//Instantiate NEW controller
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"SeeInfoVC"];
//Present NEW controller
[self presentViewController:vc animated:YES completion:nil];
You created a controller and assigned data to it, and then didn't use it anymore, instead you created a new one from the storyboard, you didn't add the data and presented it.
Basically, you created two, set data to one and presented the other one.
See the problem?
Related
I am developing iOS App.
In NavigationController, MainViewController has custom ContainerViewController(*YSLContainerViewController) which is composed of several UIViewController like the following code.
*YSLContainerViewController:https://github.com/y-hryk/YSLContainerViewController
MainViewController.m
#property (nonatomic) YSLContainerViewController *containerVC;
-(void)viewDidLoad
{
[super viewDidLoad];
CategoryViewController *categoryViewController[5];
categoryViewController[0] = [self.storyboard instantiateViewControllerWithIdentifier:#"EachCategoryView"];
categoryViewController[0].category = 0;
categoryViewController[0].title = #"apple";
categoryViewController[1] = [self.storyboard instantiateViewControllerWithIdentifier:#"EachCategoryView"];
categoryViewController[1].category = 1;
categoryViewController[1].title = #"orange";
categoryViewController[2] = [self.storyboard instantiateViewControllerWithIdentifier:#"EachCategoryView"];
categoryViewController[2].category = 2;
categoryViewController[2].title = #"strawberry";
categoryViewController[3] = [self.storyboard instantiateViewControllerWithIdentifier:#"EachCategoryView"];
categoryViewController[3].category = 3;
categoryViewController[3].title = #"cherry";
categoryViewController[4] = [self.storyboard instantiateViewControllerWithIdentifier:#"EachCategoryView"];
categoryViewController[4].category = 4;
categoryViewController[4].title = #"watermelon";
NSMutableArray *tabArray =
[NSMutableArray arrayWithObjects:categoryViewController[0], categoryViewController[1], categoryViewController[2], categoryViewController[3], categoryViewController[4], nil];
float navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
_containerVC = [[YSLContainerViewController alloc]initWithControllers:tabArray
topBarHeight:navigationbarHeight
parentViewController:self];
_containerVC.delegate = self;
[self.view addSubview:_containerVC.view];
}
Each categoryViewController has TableView. When a cell in TableView is tapped, goes to detailViewController.
CategoryViewController.m
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DetailViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"DetailView"];
[self.navigationController pushViewController:vc animated:YES];
}
Each time I repeat the above transition like
"(tap tableViewCell in) categoryViewController in MainViewController → detailViewController → (go back to) MainViewController → (tap tableViewCell in) categoryViewController in MainViewController → detailViewController → (go back to) MainViewController..."
,my app are rapidly consuming the memory , it will not be reduced.
I guess in each of transition categoryViewControllers are not released from memory.
I tried the following method, but it does not work well.
DetailViewController.m
- (void)viewDidDisappear:(BOOL)animated
{
[self.navigationController popViewControllerAnimated:YES];
([self.navigationController popToRootViewControllerAnimated:YES];)
}
Could you tell me how to solve this problem?
Here is my code in the first viewcontroller.m file:
RiskViewController *riskVC = [[RiskViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
riskVC.Address1 = #"THIS IS THE DATA IM TRYING TO SEND!";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: [NSBundle mainBundle]];
[self.navigationController pushViewController:[storyboard instantiateViewControllerWithIdentifier:#"Risk"] animated:YES];
Here is my code in the second viewcontroller.h file:
#interface RiskViewController : UIViewController <UITextFieldDelegate>
#property (nonatomic, strong) NSString * Address1;
Here is my code in the Second viewcontroller.m file. It is NSLoging that self.Address1 is null:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Addy1%#", self.Address1);
}
Why is my self.Address1 String null, it should contain my string I sent, How do I fix this so that the data gets sent?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: [NSBundle mainBundle]];
RiskViewController *riskVC = [storyboard instantiateViewControllerWithIdentifier:#"Risk"]
riskVC.Address1 = #"THIS IS THE DATA IM TRYING TO SEND!";
[self.navigationController pushViewController:riskVC animated:YES];
I have two view controller named ViewController and SecondViewController. From the first view controller i m trying to call my SecondViewController after 5 second. but it will giving me black screen instead of my expected view.
Here is my view controller code,
`
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(loadingNextView) withObject:nil afterDelay:5.0f];
}
-(void)loadingNextView{
SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end`
Please suggest. If i used button for navigation then its work but my needs is to call automatically on timer.
If your SecondViewController is in storyboard instead of alloc init you have use instantiateViewControllerWithIdentifier
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
Dont forget to give the identifier for the controller in the storyboard. So the method will be
-(void)loadingNextView{
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
If you are using storyboard then you cannot initialise the controller with the nib Method "initWithNibName" basically searches for an Xib file, which you do not have as you are using a storyboard, better initialise it in the following way ..
-(void)callNextView{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"yourStoryBoarddName" bundle:nil];
yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
[self presentViewController:VC animated:YES completion:NULL];
}
If you are not using the storyboard, you can simply init the new controller like this
SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];
You need to initialize the controller exact from the storyboard.
- (void)loadingNetView
{
UIStoryboard storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NSString *identifier = #"SecondController"; // you need to set identifier in storyboard
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
}
I want to pass some core data from master to detail.
First the user selects a name which has a gender assigned. If gender Girl "pige" it goes to the girls UITableiew. From there each selection shows on detail view.
I want to pass some data from the girl UITableiew. Like if its a girl color it pink. set a title etc.
So far i know how to pass around the UITableiew "master" but not how to get it to the detail view
Here's some code on how i manage the selection of UITableView etc.
DetailViewContainerController
UIStoryboard* initial = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
self.initital = [initial instantiateViewControllerWithIdentifier:#"initialSite"];
[self addChildViewController:self.initital];
self.startSide = [initial instantiateViewControllerWithIdentifier:#"startSide"];
[self addChildViewController:self.startSide];
-(void)initialSite:(int)viewId
{
UIViewController *viewController;
switch (viewId)
{
case 0:
viewController = self.initital;
[self.navigationController setNavigationBarHidden:NO animated:NO];
break;
case 1:
viewController = self.startSide;
[self.navigationController setNavigationBarHidden:NO animated:NO];
break;
}
[self showChildViewController:viewController];
}
-(void)showChildViewController:(UIViewController*)content
{
if(topController != content)
{
content.view.frame = [self.view frame]; // 2
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
topController = content;
}
}
startTableViewController
-(void)viewDidLoad
{
arrayA = [[[NSMutableArray alloc] initWithArray:[Start findAllSortedBy:#"navn" ascending:YES]] mutableCopy];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"gender"]isEqualToString:#"Pige"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MenuPige"];
controller.forNavn = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"gender"]isEqualToString:#"Dreng"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main-iPad" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MenuDreng"];
controller.forNavn = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
}
MenuPige
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.detailViewContainerController initialSite:[indexPath row]];
if ([[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:#"menuPunkt"]isEqualToString:#"Barnedåb / Navngiving"]) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"BarneDaab" bundle:nil];
DetailViewContainerController *controller = [storyboard instantiateViewControllerWithIdentifier:#"barneDaab"];
[self.navigationController pushViewController:controller animated:YES];
}
in the girl menu.h i have the NSStrings of the data that i want to pass. So that is working. but how to get it to detailviewcontroller ?
UPDATE:
I tried this Accessing array from a detail view controller into master view controller in Objective-C
but can't get it work
UPDATE 2:
self.navigationController.parentViewController.navigationItem.title = [self.detailViewContainerController.forNavn valueForKey:#"navn"];
doesn't work in the velkommen.m, it returns nil
UPDATE 3:
Leftmenucontroller.m - this works. but only to another uitableview. can't get it over to the detail view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewContainerController *start = [[DetailViewContainerController alloc]init];
start.forNavn = forNavn;
NSLog(#"leftview = %#", forNavn);
[self.detailViewContainerController initialSite:[indexPath row]];
}
UPDATE 4:
startTableViewController.h
{
NSMutableArray *arrayA;
NSMutableArray *tableData;
NSString *forNavn;
NSString *mellemNavn;
NSString *efterNavn;
NSString *gender;
}
#property (nonatomic, retain) NSString *forNavn;
#property (nonatomic, retain) NSString *mellemNavn;
#property (nonatomic, retain) NSString *efterNavn;
#property (nonatomic, retain) NSString *gender;
#property (nonatomic, strong) NSMutableArray *arrayA;
UPDATE 6:
I figured out that i can pass the data to DetailviewContainerController with this
DetailViewContainerController *test = (DetailViewContainerController *) [[self.splitViewController.viewControllers lastObject] topViewController];
test.leftViewController = self;
test.forNavn = forNavn;
NSLog(#"test = %#",test.forNavn);
but now i can't get it further from DetailViewContainerController. i tried this :
VelkommenViewController *test2 = (VelkommenViewController *) [[self.splitViewController.viewControllers lastObject] topViewController];
test2.detailViewContainerController = self;
test2.forNavn = self.forNavn;
NSLog(#"test2 = %#",test.forNavn);
Update 7:
Update 8:
VelkommenViewController *test2 = [[[self.splitViewController.viewControllers.lastObject topViewController] childViewControllers] objectAtIndex:0];
test2.forNavn = forNavn;
NSLog(#"test3 = %#",test2.forNavn);
Throws a error : -[UINavigationController setForNavn:]: unrecognized selector sent to instance 0x175b56c0
Update 9:
It's a little unclear what you're doing, especially with the child view controller stuff. But, in general, when working with a split view controller, the detail controller can be accessed by using self.splitViewController.viewControllers[1] (if the detail controller is embedded in a navigation controller, then that gets you a reference to the navigation controller, and you have to use its topViewController property to get to the detail controller). You shouldn't be using instantiateViewControllerWithIdentifier:, or alloc init to access the detail controller, since either of those methods will give you a new instance instead of referencing the one you have on screen.
on the receiving end this works:
NSString *name = ((DetailViewContainerController *)self.parentViewController).forNavn;
I am making an replica of Scrumptous sample app (ios facebook sdk version - 3.10 ) using Storyboard approach
I am able to load user name and profile image correctly.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do nothing for now
switch (indexPath.row) {
case 2:
if (!self.friendPickerController) {
self.friendPickerController = [[FBFriendPickerViewController alloc]
initWithNibName:nil bundle:nil];
// Set the friend picker delegate
self.friendPickerController.delegate = self;
self.friendPickerController.title = #"Select friends";
}
[self.friendPickerController loadData];
[self.navigationController pushViewController:self.friendPickerController
animated:true];
break;
}
}
With below approach we can instantiateViewController but we need a valid identifier
/*
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"myViewController"];
[self presentViewController:vc animated:YES completion:NULL];
*/