How can I get access to a property of a ViewController when I have created an object of it like this:
for (UIViewController* vc in self.navigationController.viewControllers)
{
if ([vc isMemberOfClass:NSClassFromString(#"myViewController")])
{
// change property value on viewcontroller vc, for instance: vc.myText = #"hello" ??
}
}
thanks in advance!
You need to cast to let the compiler know the data type (or cheat and set it by an indirect method):
myViewController *mvc = (myViewController *)vc;
mvc.myText = #"hello";
Note also that class names should start with a capital first letter.
Just use simply like this:
(myViewController *)vc.myText = #"hello"
myText needs to be public variable also.
Related
I had tried accessing value of IVAR of one viewcontroller to another viewcontroller by making use of property , but i am getting null value.
please let me know how to get value displayd in my secondviewcontroller.
You are doing the right way to get value of property. But you need to make sure myString has been assigned a value in init method of ViewController.
Try in ViewController.m:
- (instancetype)init {
self = [super init];
if (self) {
self.myString = #"My String";
}
return self;
}
Beside of it, make sure that myMetod of SecondViewController is called. If myMetod is called, "Print value My String" will be printed.
I know that this is double question. I know that I can use property in toViewController to get name of UIViewController to get NSString which tells me where I am coming from.
Anyway I want to ask if there a simple way to get name of UIViewController when unwinding from segue.
I have a UIViewController with segues to 3 forms. I programatically return to that view controller. I need to run a specific code only when I am returning from one of view controllers. My goal is using string from name of fromViewController start that specific code.
Using UIViewController by NSString from its class name isn't safe enough because the name can be changed.
You can use isKindOfClass instead:
UIViewController *destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[MyViewControllerClass1 class]]) {
// put code related to transition to MyViewControllerClass1
}
else if ([destinationViewController isKindOfClass:[MyViewControllerClass2 class]]) {
// put code related to transition to MyViewControllerClass2
}
You can use:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIViewController *destinationViewController = segue.destinationViewController;
NSString * identifier = destinationViewController.identifier;
NSString * title = destinationViewController.title;
}
Create a Custom delegate method in the primary VC, create 3 strings with unique name so that u can identify.
EG.
NSString* stringFrmFORM1, *stringFrmFORM2, *stringFrmFORM3;
-(void)setString:(NSString*)myString{
//set the string from the VC1,2,3 to each string based on Primary VC's strings
}
Call the delegate method from each registration VC, and set those Strings.
You will have your registration strings to each of the Unique strings that you have set, from each of the Registration VC's.
To answer your base question, you can get the name of a class in string form with:
NSString *strClassName = NSStringFromClass([fromViewController class]);
but as #AlexPeda pointed out in ze answer, -isKindOfClass: would be better.
if ([fromViewController isKindOfClass:[SpecificViewController class]]) {
//run your 'specific' code
}
I have the following:
-(void)turnBlue:(DetailVC *) controller {
self.Label.text = controller.selected.name;
self.selected = controller.selected;
[controller.navigationController popViewControllerAnimated:YES];
}
I'm trying to enter from my ibaction here:
- (IBAction)pressButton:(id)sender {
//filler code here
}
I'm trying
[self turnBlue:selected];
But am getting 'incompatible pointer types sending...'
I've tried controller, what details am I missing?
It should be something like:
// Allocate or get already created instance of `DetailVC`
DetailVC *controller = [[DetailVC alloc] initWithNibName..........];
// Pass instance to `turnBlue` function:
[self turnBlue:controller];
turnBlue can only accept a parameter of type DetailVC, while you are passing selected which seems not to be of type DetailVC so you are receiving incompetible pointer type error.
Hope it helps!
I am new to IOS Programming. I am sending an integer value to another view controller from segue like this:
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
I am successfully accessing this value in my SlideShowViewController.m file:
#synthesize selectedButton;
NSLog(#"%i",selectedButton);
Now I want to access this same value which saved in selectedButton in my other View Controller. So I did this in my viewDidLoad of AnotherViewController:
SlideShowViewController *button= [[SlideShowViewController alloc] init];
NSLog(#"selected button is %i",button.selectedButton);
But I am not getting the value here.
While going to second view controller do same as you did for first
SecondViewController *destViewController2 = segue.destinationViewController;
[destViewController2 setSelectedButton:tagIndex];
In your SlideShowViewController.h create a property to hold data.
In your SlideShowViewController.m synthesize property and remove
SlideShowViewController *button= [[SlideShowViewController alloc] init];
this line from SlideShowViewController.m. In this line your are creating new instance of SlideShowViewController.
In the last code section, you're creating a new instance of the SlideShowViewController. This instance won't have the same variables as the first instance. In order to access the selectedButton field, you need to have a reference to your first SlideShowViewController instance to access its properties.
// Here you create a new instance. You need to hang onto the destViewController
// pointer if you want to access its selectedButton property later. This means
// you'll need to use a global variable, or otherwise expose it in some way for
// future reference.
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
Later, possibly in a different method, you could do something like:
// Note, we're referring to destViewController using self. I'm assuming you've
// set up a property on the self object to refer to destViewController outside
// of the scope of the initial initialization.
int selectedButtonIndex = self.destViewController.selectedButton;
Trying to see if a UIViewController or UIView can identify its Storyboard ID. So was hoping for:
UIViewController *aViewController;
NSString *storyboardID = aViewController.storyboard.id; //not an actual property
or:
NSString *storyboardID = [aViewController.storyboard valueForKey:#"storyboardId"]; //also not a working call
But no joy and couldn't find a similar solution online. Does anyone know if this is even possible?
You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.
You can use the Restoration ID:
NSString *restorationId = self.restorationIdentifier;
Just check the checkbox 'Use Storyboard ID'
The storyboard id is only meant to find and instantiate a VC from a storyboard.
As written in the UIStoryboard reference:
"This identifier is not a property of the view controller object itself and is only used by the storyboard file to locate the view controller."
Why do you need it?
You can also try doing something like this:-
NSString *storyboardId = [viewController valueForKey:#"storyboardIdentifier"];
This will precisely give you the Storyboard Id that you have set via interface builder.
Swift extension:
extension UIViewController {
var storyboardId: String {
return value(forKey: "storyboardIdentifier") as? String
}
}
The most reliable method for returning the "id" of the UIViewController or UIView is...
NSString *viewControllerName = [[NSString alloc] initWithString:viewController.nibName];
This will return... "29w-Ic-LNo-view-FDu-oq-UpZ", where "29w-Ic-LNo" is the Object ID of the UIViewController and "FDu-oq-UpZ" is the Object ID of the UIView.
However, you may also use...
NSString *viewControllerName = [[NSString alloc] initWithString:viewController.title];
This will return the "Title" of the UIViewController in the Attributes Inspector; so just as easily as you added the Storyboard ID to the UIViewController, you may also add a title.
You can compare with class name .
import class and then try.
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *root = [viewControllers objectAtIndex:0];
if ([root isKindOfClass:[UserLogin class]]) {
//--- do ---
}