Passed NSString always null - ios

I need to pass an NSString from one view to another. I do in my first view controller:
-(IBAction)btnCreate_click:(id)sender
{
CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}
CreateMatchTableViewController is my second view controller and createBtnPressed ia NSString object in second view controller.
In viewDidLoad of second view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// MatchListViewController *obj = [[MatchListViewController alloc]init];
NSString *stringFromFirstView = createBtnPressed;
NSLog(#"check===>%#",stringFromFirstView);//...always null
}
Please help!!

Seems that in your first view controller you are creating a new instance of CreateMatchTableViewController. You do not want that because you have your CreateMatchTableViewController instance already in the tab bar controller...
Try something like this:
-(IBAction)btnCreate_click:(id)sender
{
CreateMatchTableViewController *matcObj = [self.tabBarController.viewControllers objectAtIndex:1];
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}
EDIT
-(IBAction)btnCreate_click:(id)sender
{
UINavigationController *navigationController = [self.tabBarController.viewControllers objectAtIndex:1];
CreateMatchTableViewController *matcObj = [[navigationController viewControllers] objectAtIndex:0];// I put 0 assuming you have a CreateMatchTableViewController as the navigation controller's root view controller
matcObj.createBtnPressed = #"pressed";
[self.tabBarController setSelectedIndex:1];
}

on CreateMatchTableViewController write a method like following:
(void)setStringFromFirstViewController:(NSString*)str{
NSString *stringFromFirstView = str;
NSLog(#"check===>%#",stringFromFirstView);
}
Now on "first view controller" do the following:
-(IBAction)btnCreate_click:(id)sender{
CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
[matcObj setStringFromFirstViewController: #"pressed"];
[self.tabBarController setSelectedIndex:1];
}

Try
NSString* a = [[NSString alloc] initWithString:createBtnPressed];

Related

TabBarController returns null

I have a tabbarcontroller in storyboard as initial view controller
How does this return null
UITabBarController* tabbarController = (UITabBarController*) self.tabBarController;
NSLog(#"%#",tabbarController);
NSLog(#"%#",self.navigationController.tabBarController);
Originally what I am trying to do
NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
NSLog(#"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(#"self.tabBarController.viewControllers %# \n\n",self.tabBarController);
[newTabs removeObjectAtIndex: 1];
[self.tabBarController setViewControllers:newTabs];
Why am I getting null?
The reason null is returned, is that self is your UITabBarController, therefore, it doesn't have anything on self.tabBarController
Your code should look like this:
NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self viewControllers]];
NSLog(#"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(#"self.viewControllers %# \n\n",self);
[newTabs removeObjectAtIndex: 1];
[self setViewControllers:newTabs];
In Swift
let indexToRemove = 1
if indexToRemove < (self.viewControllers?.count)! {
var viewControllers = self.viewControllers
viewControllers?.remove(at: indexToRemove)
self.viewControllers = viewControllers
}
directly use self in UITabBarController class, not self.tabBarControllers.
Check out Joe's Answer self.tabBarController is NULL
If you have a navigation controller you need to add this to your YourTabBarViewController.h file
#property (nonatomic, retain) UITabBarController * myTabBarController;
Then in YourTabBarViewController.m file in viewDidLoad just assign it to self and add the delegate
self.myTabBarController = self;
self.myTabBarController.delegate = self;

UITabbarController selectedIndex and selectedViewController do not work

I've tried to put following two lines into - (void)viewDidLoad.
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
At that time, only Tabbar index to change 3 but View Controller remained at default view controller, mean 0. Is there any thing missing in my coding?
Solution: Try by moving your,
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
portion of your code to UIVIewController's
- (void)viewDidAppear:(BOOL)animated
I hope it will help you
Reason: You have to let your UIViewController to load fully first before you trigger an extra action on it.
Try this definitely it works:
UINavigationController *nc = [[self.tabBarController viewControllers] objectAtIndex:0];
id vc = [[nc viewControllers] objectAtIndex:0];
if ([vc isKindOfClass:[YourViewController class]])
{
[[self appDelegate].tabBarController setSelectedIndex:3];
}

back button doesn't go back

After pushing a ViewController using this code
UIViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:#"frontCardViewController"];
[self.navigationController pushViewController:vc animated:YES];
I remove all ViewControllers I don't need anymore using this code
NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
NSArray *array = [[NSArray alloc] initWithArray:navigationArray];
for (UIViewController *viewController in array) {
if ([viewController isKindOfClass:[RITCardViewController class]]) {
RSLog(#"Is kind of Class RITCardViewController");
[navigationArray removeObject:viewController];
}
}
self.navigationController.viewControllers = navigationArray;
The navigation array now looks like this:
"RITMainViewController: 0x10d81fc1>",
"RITDetailViewController: 0x10d847880",
"RITTestResultViewController: 0x113d0e090"
But the problem is that if the back button in the navigation bar is pressed now, it goes back to the second screen. But when the back button is pressed again it just stays on this screen. It seems to go trough all the screens I have removed, but doesn't show them.
What am I doing wrong?
Try something like this instead:
NSMutableArray *viewControllers = [#[] mutableCopy];
for (UIViewController *vc in self.nagivationController.viewControllers)
{
if (NO == [vc isKindOfClass:[RITCardViewController class]])
[viewControllers addObject:vc];
}
self.navigationController.viewControllers = viewControllers;
Make sure you don't corrupt the stack by removing the view controller you are currently on from the view controllers array. Assuming your current view controller is not an instance of RITCardViewController you should be fine. Otherwise, you'll have to make up for it in your code.

How can i refresh my other view controllers from an action of slider

I have a UITabBar Controller having three view controller. I am trying to refresh my two view controller which are map view controller and tableview controller; from third view controller with an action of slide. Here what i have tried:
- (IBAction)actionMySlider:(id)sender{
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: mySlider.value]];
MetreLabel.text = [NSString stringWithFormat:#"Yarı Çap(Re): %#(m)",numberString];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setVariable:[NSString stringWithFormat:#"%f",mySlider.value]];
ViewController1 *VC1=[[ViewController1 alloc]init];
[VC1 viewDidLoad];
ViewController2 *VC2 = [[ViewController1 alloc]init];
[VC2 viewDidLoad];
}
Thank you for your answers.
EDIT
According to answers my approach is implementing a for loop at my TabBar.m. But i have some issues while implementing this.
for (UIViewController *v in self.tabBar.viewControllers)
{
UIViewController *vc = v;
if ([v isKindOfClass:[UINavigationController class])
{
vc = [v visibleViewController];
}
if ([vc isKindOfClass:[MyViewController class]])
{
MyViewController *myViewController = vc;
[vc doSomething];
}
}
problem: what should i write instead of viewcontrollers at self.tabBar.viewControllers. I could not figure out that.
problem: after i implement that for loop in a method in TabBar.m, will i be able to reach my viewcontrollers from another viewcontroller via calling that method?
You are allocating a new instance of both ViewController1 and ViewController2 in the actionMySlider method. They are not the same as the view controllers in your tab bar controller.
Try to access the view controller from the tab bar itself. The answer found here will do:
UITabBarController - How to access a view controller?
And you should try to take those lines of code that you want to execute in ViewController1 and ViewController2 after the slider value change out from the viewDidLoad and make them separate methods. Then you can just call those methods without calling the entire viewDidLoad.

How can we change views in a UISplitViewController other than using the popover and selecting?

I have done a sample app with UISplitViewController studying the example they have provided. I have created three detailviews and have configured them to change by the default means. Either using the left/master view in landscape AND using the popover in the portrait orientation.
Now I am trying to move to another view(previous/next) from the currentView by using left/right swipe in each view. For that, what I did was just created a function in the RootViewController. I copy-pasted the same code as that of the tablerow selection used by the popover from the RootViewController. I am calling this function from my current view's controller and is passing the respective index of the view(to be displayed next) from the current view. Function is being called but nothing is happening.
Plz help me OR is anyother way to do it other than this complex step? I am giving the function that I used to change the view.
- (void) rearrangeViews:(int)viewRow
{
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (viewRow == 0) {
DetailViewController *newDetailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (viewRow == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (viewRow == 2) {
ThirdDetailViewController *newDetailViewController = [[ThirdDetailViewController alloc] initWithNibName:#"ThirdDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
}
I have googled and found a nice approach. They use a UINavigationController on the detail view to push new views. You could use this same solution for your project.
http://www.cimgf.com/2010/05/24/fixing-the-uisplitviewcontroller-template/

Resources