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];
Related
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.
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
I customized the action of back button. I want to send to parent view a BOOL if back is pressed, but the bool value is always null.
my parent .h
[...skip...]
BOOL myBool;
[...skip....]
my parent .m
#import "theChild.h"
....
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"myBool is %d", (int)myBool);
}
-(IBAction)callTheChild:(id)sender {
theChild *theChildVC = [[theChild alloc] initWithNibName:#"theChild" bundle:nil];
// set something
[self.navigationController pushViewController:theChildVC animated:YES];
[theChildVC release];
}
in my theChild .m
#import "theParent.h"
....
....
-(void)backAction:(id)sender {
theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
[addVC release];
// dismiss child view
[self.navigationController popViewControllerAnimated:YES];
}
when the parent appear, myBool is null.
if I change
[self.navigationController popViewControllerAnimated:YES];
to
[self.navigationController pushViewController:theParentVC animated:YES];
all works fine but is not what I want for several reasons.
Any help is appreciated.
Thanks,
Max
You're not passing the bool back to the parent, you're creating a completely new object and giving that the bool instead!
Look at this line :
theParent *theParentVC = [[addSite alloc] init];
That line has made a new parent object. You probably wanted to use the original parent object :)
in theChild.h
[snip]
theParentVC *parent;
[snip]
when you create the child
-(IBAction)callTheChild:(id)sender {
theChild *theChildVC = [[theChild alloc] initWithNibName:#"theChild" bundle:nil];
[theChild setParent:self];
[self.navigationController pushViewController:theChildVC animated:YES];
[theChildVC release];
}
and when you want to update the parent
-(void)backAction:(id)sender {
// Update the parent
parent.myBool = YES;
// dismiss child view
[self.navigationController popViewControllerAnimated:YES];
}
You are creating a new viewcontroller, not linking back to the true parent.
try
self.parentViewController.myBool = YES;
rather than
theParent *theParentVC = [[addSite alloc] init];
// set parent BOOL
theParentVC.myBool = YES;
My controller hierachy:
TabBaseController (UITabBarController)
SubclassedController
In my tabbasecontroller I have a navigation bar button, which flips the subclassedcontroller with the presentModalViewController method, to a second UITabBarController.
So my question is: why does not
self.parentViewController
work in the second UITabBarController? It is nil.
I am trying this in my viewDidLoad method in the second UITabBarController:
if (self.parentViewController == nil) {
NSLog(#"Parent is nil");
}
UPDATED
This is the method in the UITabBarController with the navigationItemButton that presents it
-(IBAction)openModalTabController:(id)sender {
if (self.nvc == nil) {
ModalTabController *vc = [[ModalTabController alloc] init];
self.nvc = vc;
[vc release];
}
[self presentModalViewController:self.nvc animated:YES];
}
This is the controller(UITabBarController) that I present modally:
Header:
#interface NewBuildingViewController : UITabBarController {
}
#end
Main:
#implementation NewBuildingViewController
- (id)init {
[super initWithNibName:nil bundle:nil];
ViewController1 *vc1 = [[ViewController1 alloc] init];
ViewController2 *vc2 = [[ViewController2 alloc] init];
ViewController3 *vc3 = [[ViewController3 alloc] init];
NSArray *controllers = [[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil];
[vc1 release];
[vc2 release];
[vc3 release];
self.viewControllers = controllers;
[controllers release];
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
return [self init];
}
#end
I also want to add that this message is displayed in the console(warning) when flipping:
Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
Using two-stage rotation animation is not supported when rotating more than one view controller or view controllers not the window delegate
It would be helpful if you were to show how you are presenting that second UITabBarController. Are you perhaps ignoring the following warning found in the UITabBarController class reference?
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
I want to ask a question about the iPhone application. I write a program which will display a table. However, I don't know why I cannot display the navigation title in the table. The following is my code
// code
- (void)viewDidLoad {
[super viewDidLoad];
// control flow, call another user define function and add the items to the table
[self flowControl];
//Set the title
self.navigationItem.title = #"Table Title Hello";
// can see
// NSLog(#"self.navigationItem.title: %#", self.navigationItem.title);
}
The table can only display the item but not the title. Can any one help me?
// ---------- Update 1 --------------
The code in the [self flowControl] will call two functions, both of the function is to add the items, e.g [displayNameArray addObject:#"John Chan"];
// ---------- Update 2 ---------------
In my program, the are 2 view controllers.
1) MyViewController
2) SimpleTableView
The first one is used to let the user to enter the information, the second one is used to display the content in table format.
My project name is USERPROJECT
//The following is the content of the .m
#import "MyViewController.h"
#import "USERPROJECT.h"
#import "SimpleTableView.h"
#implementation USERPROJECT
#synthesize window;
#synthesize myViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[myViewController release];
[window release];
[super dealloc];
}
#end
And it is one of the content within the 'MyViewController.m', I use this to switch to the control from the 'MyViewController.m' to 'SimpleTableView.m'
- (void) switchPageShowTable {
NSLog(#"%d: switchPageShowTable", order);
order++;
SimpleTableView *simpleTableView = [[SimpleTableView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:simpleTableView animated:YES];
}
Is it affect the use of the 'self' in the calling title? Thank you very much.
The title you expect to see is displayed in a navigation bar, which is usually part of UINavigationController. To make it work you have to do the following:
SimpleTableView *simpleTableView = [[SimpleTableView alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: simpleTableView];
[self presentModalViewController:navController animated:YES];
[simpleTableView release];
[navController release];
And (a small nitpick) instead of
self.navigationItem.title = #"Table Title Hello";
you can do this:
self.title = #"Table Title Hello";
unless you have a reason to explicitly use the navigationItem.
Is the view your trying to load a subclass of UITableViewController?
And what does [self flowControl] do exactly?