i have a method in delegate.m file
-(void) switchToTabbarController
{
TabBarController *tabBarController =
[[TabBarController alloc] initWithNibName:#"TabBarController" bundle:nil];
[self.window addSubview:tabBarController.view];
}
and i wanted to call this method from my LoginView Method. How to do it?
At first you have to import the header, and then create the object of your Delegate class, and then call the method. It will look like this:
#import "Delegate.h"
In place where you want to call it:
Delegate* del=[[Delegate alloc] init];
[del switchToTabbarController];
And after you are done, I would strongly suggest reading Apple's Objective-C Programming Guide:
Link.
EDIT: if this is your AppDelegate, go with Mats' solution.
First I think you do not want to call it this way. I prefer not to call the UIApplication from a view and try to prevent it from a controller. Use a notification instead.
But the way you could do this is:
[(ApplicationDelegate *)[UIApplication sharedApplication].delegate switchToTabbarController]
include header file -
#include
create instance of this class
delegate *d=[[delegate alloc] init];
[d switchToTabbarController];
Related
I am using the option to play some media, if I link it to an IBAction on ViewController.h it will play fine.
The problem occurs, when I try to call that, from another ViewController, for example;
ViewController *myViewController = [[ViewController alloc] init];
[myViewController showVideos];
This is called from SecondViewController and refers to the code in ViewController.m
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
It works using it if I am viewing it on the ViewController, but the call using the
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
Throws the error about window hierarchy when calling it from the SecondViewController.m file
Now, in the SecondViewController.h file, the only reference to ViewController, is a simple import of the .h file, should I be initialising it or giving it a property in there also?
This: ViewController *myViewController = [[ViewController alloc] init]; creates a new ViewController object. You then don't present it (i.e. don't give it access to the screen) but you ask it to showVideos.
I suspect what you really want is to get a reference to an existing ViewController. When you create your SecondViewController give it a reference to the first one to act as a delegate.
I tried to save or load some data when my app is terminating. In my ViewController.m I made 2 functions, one for saving and one for loading.
In my AppDelegate.m I tried to access my ViewController, but it doesn't seem to work..
I tried a couple of different ways that I found here on stack overflow, but the Delegate doesn't recognize it as a ViewController or something:
[self.ViewController myFunction];
[self.rootViewController myFunction];
[self.window.ViewController myFunction];
They all don't work. What am I doing wrong? Am I using the wrong name for the ViewController?
I'm using storyboards by the way.. Is the accessing method different here?
First You need to do initialization of object of your self.viewController such like
self.ViewController = [[ActualNameOfViewController alloc] init];
And then call method such like
[self.ViewController myFunction];
Might be this is helpful :)
First you need to make sure that ViewController is imported than do alloc and init
////#import "ViewController.h"
ViewController *MyVc = [[ViewController alloc]init];
[MyVc myFunction];
Import your UIViewController class in AppDelegate.m class :
#import SampleViewController.h
after that create an instance of UIViewController:
self.sampleViewController = [[SampleViewController alloc]
initWithNibName:#"SampleViewController"
bundle:nil];
[self.sampleViewController myFunction];
SampleViewController is xib name for that UIViewController.
I have TabBar based iPhone application, and in app delegate 2 default view controllers are initialized by apple (if you choose tabbar base app when creating application).
UIViewController *rootViewController = [[tabBarBetFirstViewController alloc] initWithNibName:#"tabBarBetFirstViewController" bundle:nil];
UIViewController *accountViewController = [[tabBarBetSecondViewController alloc] initWithNibName:#"tabBarBetSecondViewController" bundle:nil];
Why this isn't initialized like this:
tabBarBetFirstViewController *rootViewController = [[tabBarBetFirstViewController alloc] initWithNibName:#"tabBarBetFirstViewController" bundle:nil];
tabBarBetSecondViewController *accountViewController = [[tabBarBetSecondViewController alloc] initWithNibName:#"tabBarBetSecondViewController" bundle:nil];
???
Is that the same ? Or it's just those default that are added by apple? If i want to add one more tab will I write:
UIViewController *third = [ThirdViewController alloc].....];
or
ThirdViewController *third = [ThirdViewController alloc]....];
Of course at the end I have:
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:rootViewController, accountViewController, third, nil];
ThirdViewController is a subclass of UIViewController, so you can write both. But if you later want to use the variable third to invoke methods that are specific to ThirdViewController, then you should use
ThirdViewController *third = [ThirdViewController alloc]....];
Summing it up: In this simple scenario there is no single right way of "doing it". The important lesson to take from this question (if it wasn't clear already) is to understand why you can assign a ThirdViewController instance to a UIViewController variable (because of the subclassing relationship).
You use the
ThirdViewController *third = [ThirdViewController alloc]....];
approach. Don't know why Apple uses the other approach. I this easy example it doesn't make any difference. But when you have properties you want to set it's better to use the class name.
It depends, if you have a view controller that you want to have a custom interface, you will want it to be a subclass of UIViewController. If ThirdViewController is a subclass of UIViewController then that code that you stated here:
ThirdViewController *third = [ThirdViewController alloc]....];
Would produce the desired result. Apple's approach is just for a generic View Controller without any properties, so ideally you would want all of your tabs to be UIViewController subclasses.
1) If you want to make use of any instance methods or properties in your ThirdViewController, then you must use
ThirdViewController *third = [ThirdViewController alloc]....];
2) If you dont have a need to do so, you can use
UIViewController *third = [ThirdViewController alloc]....]; // it'd make no difference
To be on a safer side, imo, first case is a good practice.
In this case I do not see any difference, I would prefer doing it your way. But in a situation similar to the one below, Apple's way seems better:
UIViewController *vc;
if ( some_case ){
vc = [YourViewController1 alloc]// ...;
[ (YourViewController1 *) vc doSomeThing]; // You might need to use casting for instance messages
//...
}
else {
vc = [YourViewController2 alloc]//...;
}
[self.navigationController pushViewController:vc animated:YES];
[vc release];
I have an object that is used between all my viewControllers so I have stuck it inside the application delegate. (I assume this is the correct place?).
If an action inside a viewController fires that needs to send something to said object I am performing the following:
- (IBAction)sliderMoved:(id) sender{
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[[delegate myObject] setSpeed:(int)slider];
// [delegate release];
}
I am a bit concerned I am not releasing the delegate object anywhere, is this ok? If I remove the commented line [delegate release] it just crashes the application.
You don't own the delegate object that you create in this snippet. You haven't created it with alloc, new, or a copy. Nor have you retained it. So, it is not your responsibility to release it.
As for putting an object in the Application delegate just to be able to access it from other parts of your code - that is poor OOP design IMO.
Edited to add
I suppose I had better give an example
Suppose you have a class MyClass that you want to create an object of that you can pass around.
You create it in the Application delegate, which it seems you are already doing:
MyObject *myObject = [[MyObject alloc] init];
Then you create another view controller - which you would normally do, except that this view controller has a property:
#property(retain) MyObject *object;
And then you set this property when you create the view controller:
YourViewController *vc = [[YourViewController alloc] init];
vc.object = myObject;
And, if you pass this object to other view controllers as you require.
basically i have parsed some data from XML into a NSMutableArray that is shared in the appDelegate.
in my secondViewController i have a uiPickerView that i am wanting to load the details of the array into it.
My question is... how?
i have briefly worked with uiPickerView's before and had to load the data in first to assign to the uiPickerView like so:
titleDB = [[NSMutableArray alloc] init];
[titleDB addObject:#"MR"];
[titleDB addObject:#"Mrs"];
[titleDB addObject:#"Ms"];
[titleDB addObject:#"Miss"];
[titleDB addObject:#"DR"];
[titlePickerView selectRow:1 inComponent:0 animated:YES];
but since the data is coming from the appDelegate i don't know how i should load it into the uiPickerview, is it something to do with the datasource?
I'm asking to throw code at me I'm just asking for the best way to do it.
Any help on this would be great
Thanks
Jonn4y
This is a common pattern. You will want to access UIApplication's sharedApplication instance. So assuming your appDelegate class is named YourAppDelegate, the array ivar in YourAppDelegate and viewController is titleDB then you could do this in your viewController's viewDidLoad method
YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
// assuming you are using #property and #synthesize for your ivars
self.titleDB=appDelegate.titleDB;
Good luck
Basically, you want to access the appDelegate object from random places in code. This is not an unusual request. Remember that Objective C is a superset of the C language. And as such, you can use a global variable. What more natural variable would there be in a Cocoa program than the app delegate, for the reasons stated above. So, in your appDelegate .h file, add:
<MyAppDelegateClass> * appDelegate;
Substitute MyAppDelegateClass for the name of your appDelegate class name. Then just include your appDelegate's .h file anywhere you want to use the appDelegate variable, and just use (in your example):
[appDelegate titleDB]
or create a local iVar:
NSMutableArray * titleDB = [appDelegate titleDBData];
Then in your app delegate method didFinishLaunchingWithOptions, add the following line:
appDelegate = self;