How do I call a method from another view controller - iphone

I want to call this method:
- (void)getUserFriendTargetDialogRequest {
currentAPICall = kAPIFriendsForTargetDialogRequests;
[self apiGraphFriends];
}
from this conditional statement that's in another viewcontroller:
if (idx == 2) {
NSLog(#"you touched menu 2");
APICallsViewController *apiViewController = [APICallsViewController alloc];
[self.navigationController pushViewController:apiViewController animated:YES];
}
can anyone help with the syntax on this?
thanks so much

First, you probably also want to init your APICallsViewController via:
APICallsViewController *apiViewController = [[APICallsViewController alloc] initWithNibName:nil bundle:nil];
Then if that getUserFriend... is a method of APIViewController, you can do this:
[apiViewController getUserFriendTargetDialogRequest];
However, since you aren't passing any arguments in from your other view controller, you might consider calling it in the init method, or the viewDidLoad method of APICallsViewController.

OtherViewController *viewController = [[OtherViewController alloc]
init];
[viewController methodName];
[viewController release];

1>Just alloc the class in which the method is and then call it through object of that class.
ViewControllWithMethod *view=[ViewControllWithMethod alloc]]init];
[view getUserFriendTargetDialogRequest];
2>Instead of instance method you can make it as class method then you will be able to call it through className.getUserFriendTargetDialogRequest

Related

Where to initialise my TableView DataController?

I want to have a UITableViewController controlling my TableView.
Where (and how) should I call init on the UITableViewController?
EDIT
Here's my current code inside my prepareForSegue method:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
ReccyTableViewController *tableViewController = (ReccyTableViewController *)[[navigationController viewControllers] objectAtIndex:0];
ProjectDataController *aDataController = [[ProjectDataController alloc] init];
[tableViewController setDataController:aDataController];
[self presentViewController:tableViewController animated:YES completion:nil]; //Fails here
EDIT #2
Here's what I did in the end:
I scrapped trying to init the datacontroller in the prepareForSegue method and did it in the viewDidLoad method instead:
- (void)viewDidLoad
{
[super viewDidLoad];
_dataController = [[ProjectDataController alloc] init];
}
this may help you
read apple official docs
You should call it from the point of your previous controller from where you want to add/push this tableViewController.

navigate from one page to another page onclick

I would like to know a method for navigating from one page to another page onclick .I
have tried
[self.navigationController
pushViewController:self
.objectNewlist
animated:YES];
but it is showing the error
Incompatible pointer types sending 'newlistplist *' to parameter of type 'UIViewController *'
my main class file is a subclass of UIViewController
and the second class is a subclass of UITableViewController
can any one help me out with this problem?
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
-(void)onClickButton
{
ViewControllerClass *objViewControllerClass = [ViewControllerClass alloc] init] ;
[self presentModalViewController:objViewControllerClass animated:YES];
[objViewControllerClass release];
}
objViewControllerClass (object of another class)

How can I pass a parameter into a view in iOS?

UIViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
Here's my code for showing my view. I know I can use app delegate variables, but it would be neater is I could pass a parameter in somehow, ideally using an enum. Is this possible?
Just create a new init method for your HelpViewController and then call its super init method from there...
In HelpViewController.h
typedef enum
{
PAGE1,
PAGE2,
PAGE3
} HelpPage;
#interface HelpViewController
{
HelpPage helpPage;
// ... other ivars
}
// ... other functions and properties
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page;
#end
In HelpViewController.m
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle onPage:(HelpPage)page
{
self = [super initWithNibName:nibName bundle:nibBundle];
if(self == nil)
{
return nil;
}
// Initialise help page
helpPage = page;
// ... and/or do other things that depend on the value of page
return self;
}
And to call it:
UIViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil onPage:PAGE1];
[self.navigationController presentModalViewController:theController animated:YES];
[theController release];
I generally just have certain variables in my UIView, which I set from the parent view. To pass variables back, I make use of the function:
[[[self.navigationController viewControllers] lastObject] setFoo:foo];
Define a setter for the parameter in HelpViewController and change your code to:
HelpViewController *theController = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[theController setSomeValue:#"fooBar"];
[self.navigationController presentModalViewController:theController animated:YES];

Create a new ViewController object only if it doesnt exist

I use the following code to bring up a view
-(IBAction) openSomeView{
SomeView *sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
How can I detect if this view has been created already and if so, then just show it now create a new object?
Thanks
first declare in #interface
SomeView *sv;
and the you can check it
-(IBAction) openSomeView{
if(sv==nil)
sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
You cannot create more than one instance using this code, since you present modally the view controller.
Else, you'd probably keep a member variable in your class and check it against nil.
EDIT: or you can implement the 'Singleton' design pattern, if that's the meaning you search.
I have a couple of heavy-weight view controllers in my app. Right now I'm handling this situation as follows:
save MyViewController to appDelegate
in "openSomeView" method I'm checking if MyViewController was already created, if no - create it:
-(IBAction) openSomeView {
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if ( !appDelegate.myViewController ) {
appDelegate.myViewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController"
bundle:nil];
}
[self presentModalViewController:appDelegate.myViewController animated:NO];
}

EXC_BAD_ACCESS when calling pushViewController

I have a UITableView inside a UITabBarController.
When I'm calling
[[self navigationController] pushViewController:myViewController animated:YES];
there is no problem.
But, when I'm calling the same line from inside a UIActionSheetDelegate, for example:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
I get EXC_BAD_ACCESS.
It seems that calling this line from a different thread causing this issue.
How can I prevent this EXC_BAD_ACCESS issue?
(notice that myViewController is NOT nil, or something like that)
thanks!
EXC_BAD_ACCESS is thrown when you try to access a released object, and actionSheet:clickedButtonAtIndex: is called on the main thread, after the action sheet is dismissed, so I'm guessing what's pointed by myViewController is released.
It doesn't have to be nil, in fact, that's the problem. The object pointed is released, but the pointer is not nil.
I just had the same problem and mine was that I did not assine the member varible with self when I alloced it.
This causeed a problem: (myTestViewController is the class member)
TestViewController* test = [[TestViewController alloc] init];
myTestViewController = testViewController;
[testViewController release];
[[self navigationController] pushViewController:myTestViewController animated:YES];
But when I change and assied the class member with self it worked.
TestViewController* test = [[TestViewController alloc] init];
self.myTestViewController = testViewController;
[testViewController release];
[[self navigationController] pushViewController:myTestViewController animated:YES];
I had a similar problem. The way I fixed it was by removing self.navigationController.delegate = self; from my pushed view controller.
tnx.
The UIActionSheet is called from within a class called: MainRootViewController
myViewController is a member in this class (MainRootViewController).
The full code is:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
myViewController = [[myViewController alloc] init];
[[self navigationController] pushViewController:myViewController animated:YES];
}