pass data between UIViewControllers - ios

Leveraging RESideMenu https://github.com/romaonthego/RESideMenu in my app, now has an issue of passing some urlString related to lists in sideMenuViewControll to show its web page in right UIWebView.
Original codes like below:
case 2: {
navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
[self.sideMenuViewController hideMenuViewController];
}
break;
"second" is for secondViewController that has a UIWebView for showing web page at right side. secondViewController has a property: urlString.
My intention is to show its web page when selecting "Profile". Tried several way, however, I can't find one to set urlString after navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
How can I set and pass urlString so that it could be used like NSUrl * url = [NSURL URLWithString:urlString]?

First of all import your secondViewController in your first View Controller's .m file
Create in secondViewController's .h file
#property (strong, nonatomic) *NSString passedURL;
Then give the storyboard name in Attribute Inspector.
Now, on button event of profile or whatever
secondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
svc.passedURL = urlString;
[self presentViewController:svc animated:YES completion:nil];
Thats it. Hope that it works for you.

The Below line shows that,
navigationController.viewControllers = #[[self.storyboard instantiateViewControllerWithIdentifier:#"second"]];
You are trying to access the array of ViewControllers, but I am not sure why you want all the ViewControllers. I think you can get the secondViewController like this below and set the string
secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
secondViewController.urlString = urlString;

After tried every method I could get, find a workaround by using [navigationController.viewControllers[0] setTitle:urlStrings] might works.
Is doing such correctly?

Related

iOS passing data between two Views

I'm using two views (view controllers VC1 and VC2) and passing data from VC1 to VC2
I stored the values for two variables in VC2
//VC1 store obj to VC2
self.VC2.twtid=ide;
self.VC2.urlString=[[NSURL alloc] initWithString:vurl];
I received the values after VC2 was shown.
My problem is when I click in any cell in VC1 for the second time.
When I navigate back to VC1(tableview) again and tried to select cell, the values aren't stored again.
//VC.h I declare the two var
#property(strong, nonatomic) NSNumber *twtid;
#property(strong, nonatomic) NSURL *urlString;
note: I didn't use prepareForSegue.without Segue!!
Use global variables othereise use Segue methods.
ViewController2 *VC2 = (ViewController2 *)[self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
VC2.url = urlString; // use your variable over here, this is just and example
[self presentViewController:VC2 animated:YES completion:^{ }];
[self.navigationController pushViewController:VC2 animated:YES];
user presentViewController or pushViewController as per your requirement
You don't need to store the VC2 as VC1's property, just init it every time.

Accessing tableViewController from appDelegate after adding SWRevealViewController

I have an app which displays a simple tableview and I wanted to add the SWRevealViewController as well.
In my appDelegate, before I added the SWReveal VC, I was setting my tableViewController like so...
In didFinishLaunchingWithOptions:
STRTableViewController *tableViewController = [(UINavigationController *)self.window.rootViewController viewControllers][0];
self.delegate = tableViewController;
and then again in the below method:
- (void)loadTableViewData
{
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
STRTableViewController *tableVC = navVC.childViewControllers[0];
[tableVC loadTableData]
}
Obviously when I put the SWRevealViewController to the front of the line, this no longer works as it is now trying to call loadTableData from the wrong view controller.
I've tried several ways and keep coming up short. How do I go about accessing the tableViewController now that it is not the first view controller?
If you need more code or logs or anything I'll be happy to post additional info. I have a feeling the answer is right there, I just don't have the experience to see it.
Also, just to be clear, now in the storyboard it goes from Reveal View Controller to Navigation Controller (the tableview's nav VC/ sw_front) and also to the sw_rear VC. Before it simply started with the Navigation Controller.
Thanks!
There's a bunch of ways you can go about keeping a reference to this.
The simplest would be just to keep a reference to the view controller in the AppDelegate.m
So you add a property
#property (nonatomic, strong) STRTableViewController *tableViewController;
Then, whenever and wherever you are instantiating and setting that table view controller, just do something like:
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.tableViewController = justCreatedTableViewController;
You'll need to #import "AppDelegate.h" to access the app delegate in other classes where you want to do this.
Then to access it you can just do something like:
- (void)loadTableViewData
{
[self.tableViewController loadTableData]
}

showing one viewController from another viewController, when both are part of a UITabBarController

I have a UITabBarController that contains a UINavigationController each as TabbarItems.
Each UINavigationController has it's own UIViewController as the rootViewController.
Now, one of the rootViewControllers processes some information and sends it to another rootViewController.
Then, this rootViewController further processes that info and displays it on it's UI.
Till now, i have done this:
//from the current VC, taken the reference of the viewController
//to which i have to pass the data.
UINavigationController *controller = (UINavigationController *)self.parentViewController;
UITabBarController *cont = (UITabBarController *)controller.parentViewController;
//called the method of the target viewController that will do further processing
//(to which i will pass the data).
CustomerCareViewController *customerCare = (CustomerCareViewController *)[[cont viewControllers] objectAtIndex:0];
[customerCare setSRNumber:SRNum];
Now, I need to display the processed info (that will be shown on the UI of the target controller)
If you just want to switch from one tab to the other, use
self.tabBarController.selectedIndex = newIndex;
try this
[objAppDel.tabBarController setSelectedViewController:[[objAppDel.tabBarController viewControllers] objectAtIndex:1]];
hope this will help you.

Failure to pass variables/values to another view controller (not working)

Object: change the label's text in ViewControllerB
(In the ViewControllerA.m)
- (IBAction)ReturnToMenu
{
ViewControllerB *ViewControllerB =
[self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
ViewControllerB.scorelabel.text=#"WHY IS IT NOT WORKING";
[self presentViewController:GameOverViewController animated:YES completion:nil];
}
Well, this isn't working. I've exhausted all my options. This is such an elementary question, but please help me out.
P.S. #"Why is it not working" is just a temporary thing, a variable from ViewControllerA is supposed to take its place.
P.P.S. I tried out Passing Data between View Controllers, but it didn't work. I'm not sure what I'm doing wrong here
You should not try to manipulate another view controller's views directly. It's bad design, and in many cases (like yours) it doesn't work.
Instead of setting a label's text, add a string property "score". Set that after instantiating the VC.
Then, in "ViewController8"s viewWillAppear method, install the score property into the label.
Problem solved.
Here I present a modal view controller from my main view controller to show locations which I previously got from foursquare api.
LocationPickerVC *vc = (LocationPickerVC*)[self.storyboard instantiateViewControllerWithIdentifier:#"modal"];
if([vc respondsToSelector:#selector(setLocations:) ])
vc.locations = items;
[self.formSheetController presentViewController:vc animated:YES completion:nil];
[EDIT]
Make sure you set storyboard id in your storyboard (for the code below it is "modal"). Also my LocationPickerVC has this interface.
#interface LocationPickerVC : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property(strong, nonatomic) NSMutableArray *locations;
#end
OK, try this:
In your ViewController1.h that the property score is declared, add this:
+ (ViewController1 *) sharedController;
In your ViewController1.m add this method:
+ (ViewController1 *)sharedController
{
static ViewController1* staticVar = nil;
if (staticVar == nil)
staticVar = [[ViewController1 alloc] init];
return staticVar;
}
Now, where ever in other ViewControllers you need something from ViewController1, import the header, and then get the property like this:
[[ViewController1 sharedController] score]

How do I link an array between two ViewControllers?

I'm a beginner at C# so make sure you explain everything.
Okay, so I have a UIPickerView with its corresponding array of items in my ViewController. When the user clicks "submit", the app is supposed to take them to a SecondViewController, and display the item that they chose in the UIPickerView in a label. The only problem I am having is that I can't seem to link the two ViewControllers together.
I imported my ViewController by using #import into the SecondViewController, but that didn't work. I am getting a "use of undeclared identifer' error. What am I supposed to do to link the two ViewControllers together?
Thanks in advance.
If your first ViewController is the one that's presenting your second ViewController, then you can add a property in the second-view-controller that will be set to the selected value.
In SecondViewController.h
#property (nonatomic) NSString *selectedItemName;
Then, when you're presenting your second-view-controller, also set the value for this property:
SecondViewController *vc2 = [[SecondViewController alloc] init];
vc2.selectedItemName = #"selected item"; //set the actual selected item's value here
[self presentViewController:vc2 animated:YES];
Refer to this answer by Parth Bhatt. I think that should help you:
Passing NSString value between classes
I am re-pasting the answer to make the answer useful in case the link goes off in the future.
Answer from the above link:
Let us use the names of the two viewControllers as FirstViewController and SecondViewController.
Now suppose you push SecondViewController from FirstViewController on a button click then you need to write this code under your button click event:
// In the FirstViewController
- (void)buttonClicked:(id)sender{
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle: nil];
second.secString = firstString;
[self.navigationController pushViewController:second animated:YES];
[second release];
}
Here you need to declare in SecondViewController.h file:
NSString *secString;
and then create a #property
#property (non atomic,strong) NSString *secString;
In SecondViewController.m you need to #synthesize the secString:
#synthesize secString;
Hence this way you also create a getter and setter for secString by creating property and synthesizing it.
Now you can easily access the secString and you can use it anywhere in SecondViewController.
Just to check Try and check if value of firstString is passed to secString, write following code on viewWillAppear: of SecondViewController
NSLog(#"secString: %#",secString);
Let me know if you need more help.
Hope this helps.

Resources