EDIT: Added Source Project
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
I created a Split View-based Application. I then added a second UINavigationController to the DetailViewController inside the MainWindow.xib.
I then pop a new UIViewController Subclasses when a toolbar item is clicked. I use the following code to conduct the pop:
DocumentDetailVC *DetailViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
[detailViewController.detailNavController pushViewController:DetailViewController animated:YES];
DocumentsVC *RRequestViewController = [[DocumentsVC alloc] initWithNibName:#"DocumentsVC" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:RRequestViewController animated:YES];
This works. The issue I am having is how do I pass information or function calls from the Main side of the Split View to the Detail side of the Split view?
If I present the UIViewController via the following method:
DocumentDetailVC *RRequestViewController = [[DocumentDetailVC alloc] initWithNibName:#"DocumentDetailVC" bundle:[NSBundle mainBundle]];
RRequestViewController.delegate=self;
RRequestViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[RRequestViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:RRequestViewController animated:YES];
[RRequestViewController release];
RRequestViewController = nil;
I am able to complete a reach back through a protocol as intended.
DocumentDetailVC, when loaded via the pushViewController, the hierarchy is as follows:
NSLog([NSString stringWithFormat:#"%#",self]);
//self = <DocumentDetailVC: 0x4e0d960>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController]);
//self.parentViewController = <UINavigationController: 0x4e0ce30>
NSLog([NSString stringWithFormat:#"%#",self.parentViewController.parentViewController]);
//self.parentViewController.parentViewController = <UISplitViewController: 0x4e0d0f0>
Thank you for your assistance. This problem is consuming my life!
--> I uploaded a sample project that clearly shows my dilemma which can be found here <--
Ok, I know am very late for answer but this answer is, I think, perfect and working. Try it. Open your RootViewController.h, on the top write this line:
#import "Left.h"
#import "Right.h"
in #interface RootViewController write this lines
Left *lefty;
Right *righty;
after that declare property as,
#property (nonatomic, retain) Left *lefty;
#property (nonatomic, retain) Right *righty;
go to ROOTVIEWCONTROLLER.M file synthesize as,
#synthesize lefty;
#synthesize righty;
after that in RootViewController.m just replace your function with this one
- (IBAction)myBtnPushed{
NSLog(#"myBtnPushed");
lefty = [[Left alloc] initWithNibName:#"Left" bundle:[NSBundle mainBundle]];
righty = [[Right alloc] initWithNibName:#"Right" bundle:[NSBundle mainBundle]];
lefty.delegate=righty;
[self.navigationController pushViewController:lefty animated:YES];
[detailViewController.navigationController pushViewController:righty animated:YES];
}
in delloac write this,
[lefty release];
lefty = nil;
[righty release];
righty = nil;
It's done, run your app. I tested, it's done.
Related
I've tried every solution on Google and nothing seems to work. So far I've implemented a UINavigationController with the App Delegate, now all I want to accomplish is changing to the WebViewController by clicking the UIButton I've created in the interface builder, but the button doesn't seem to do anything when I run the application. Keep in mind that I want it to push to my WebViewController view.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
- (IBAction)createFile:(id)sender {
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
[self.navigationController pushViewController:webViewController animated:YES];
}
Note: In the interface builder I've already connected createFile to the button.
I understand that this is usually something that comes known as super easy but for some reason I've just never gotten it to work. Thanks in advance.
EDIT: I added the retaining property, sythesized it and added to my code in the ViewController.m file:
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:webViewController];
[self.navigationController pushViewController:webViewController animated:YES];
But now the app crashes on the button click and returns with a SIGABRT: "Pushing the same view controller instance more than once is not supported (WebViewController: 0x1ed70e80)"
give this a try
-(IBAction)createFile:(id)sender{
WebViewController *webViewController = [[WebViewController alloc]
initWithNibName:#"WebViewController" bundle:nil];
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:webViewController animated:YES];
}
One thing i noticed is that you are not retaining your navigationController in the appDelegate. So what may be happening is that your the navigationControllers view may be present and retained (from the all to addSubView) but the navigation controller may have been deallocated.
in your AppDelegate try making the Navigation Controller a retaining property
#property (nonatomic, strong) UINavigationController *navigationController
in your .m file
#synthesize navigationController
and then
Edit: Updated for pushing a view controller
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
[self.navigationController pushViewController:webViewController animated:YES];
I am new to the objective C programming and I am in a position where I need to create an iPhone App really quickly.
I am using XCode 4.2
I have a problem transferring an NSString variable from one view to the other .
the two views are in two different sets of .h and .m classes
in the first class in the .h i have something like this
#interface firstview : UIViewController {
NSString *test;
}
-(IBAction)testbutton
#end
in the .m of the firstview I have
-(IBAction)testbutton{
secondView *second;
[second setText:text]; //set text is a function that will take an NSString parameter
second= [[secondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
in the .h of the secondView I wrote
#interface secondView : UIViewController{
-IB
}
You've got the right idea, but you're trying to call -setText: before second points to a valid object! Do this instead:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
Also, the interface you give for your secondView class looks both incorrect and incomplete -- I'm not sure what you're trying to do with the -IB part. And it'd help in the future if you follow the usual Objective-C naming convention and start class names with an uppercase character: SecondView instead of secondView. Finally, I'd advise against naming a view controller ending in "...View", since that makes it easy to confuse the view controller with a UIView. All together, it should look something like this:
#interface SecondViewController : UIViewController{
NSString *text;
}
#property (retain, nonatomic) NSString *text;
#end
Declaring text as an instance variable is optional there -- if you don't do it, the compiler will create an ivar if you synthesize the accessors for your text property.
Change your code to this:
-(IBAction)testbutton{
secondView *second;
second = [[secondView alloc] initWithNibName:nil bundle:nil];
[second setText:text]; //set text is a function that will take an NSString parameter
[self presentModalViewController:second animated:YES];
}
In your original version, you were initializing (i.e. instantiating) your second view after calling setText: on it. You need to initialize it and then set the text.
You need to set the text after you allocate and initialize.
-(IBAction) testButton {
secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease];
[second setText:text];
[self presentModalViewController:second animated:YES];
}
I'm going mad using navigation controllers on the iPhone.
I have an app, with a main xib (the one with the window) inside wich I have put a NavigationController, inside wich I have a viewController. Everything is connected and the ViewController is defined with the correct inherited class name.
In the didFinishLaunchingWithOptions, i have :
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
In the .h I have :
#interface MainAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet UINavigationController* navigationController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) UINavigationController* navigationController;
#end
Then in the First ViewController I have a button connected to this method :
- (IBAction) definePreferences:(id)sender {
PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:#"Preferences" bundle:nil] autorelease];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
}
all items in the main xib seems to be connected... and retained by the properties. The AppDelegate with its window and navigationController... the Window rootviewcontroller with the same navigationController... and the file owner with the app delegate...
Everything runs fine, but the preferences window never appears...
Can you see why ?
If needed, I must say that this first view controller makes the camera interface appear and put an overlay over it. The button is onto this overlay. The imagePicker is show like this in viewDidAppear :
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
[picker release];
EDIT :
In viewDidAppear, self.navigationController is ok at the start and end of method.
In definePreferences, self.navigationController is nil. Nothing is called beetween those two calls.
Nothing
EDIT :
The problem may come from the way I init the viewController on which the button is.
Here is the method called from the firstView called by the Navigation Controller.
- (void) viewDidAppear:(BOOL)animated {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
// Insert the overlay
OverlayViewController* overlayController = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
picker.cameraOverlayView = overlayController.view;
// Show the picker:
[self presentModalViewController:picker animated:NO];
[picker release];
[super viewDidAppear:YES];
}
But... how should I do ?
First, never call an IBAction setPreferences:. That violates KVC, and can eventually cause all kinds of bizarre behaviors. setX: is a reserved name for the setter of the property named x.
You should not be creating a nav controller in this method (i.e. navController). You should be using the one you created in the NIB (self.navigationController). Check if that is nil. If it is, then you either didn't set up a navigation controller in the NIB, or you didn't wire it to this view controller.
You should also verify that nextWindow is non-nil.
I've finaly solved the problem.
See https://stackoverflow.com/questions/5317968/iphone-camera-overlay-going-down-after-a-modal-view-transition
I have some hair less... :-)
I'm using two UIViewController in Application Delegate and navigating to UIViewController using presentmodalviewcontroller. But Problem is that presentmodalviewcontroller works for first time UIViewController and when i want to navigate to second UIViewController using presentmodalviewcontroller then its showing first UIViewController.
The following is the code:-
-(void)removeTabBar:(NSString *)str
{
HelpViewController *hvc =[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:[NSBundle mainBundle]];
VideoPlaylistViewController *vpvc =[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:[NSBundle mainBundle]];
if ([str isEqualToString:#"Help"])
{
[tabBarController.view removeFromSuperview];
[vpvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:hvc animated:YES];
[hvc release];
}
if ([str isEqualToString:#"VideoPlaylist"])
{
[hvc dismissModalViewControllerAnimated:YES];
[viewController presentModalViewController:vpvc animated:YES];
[vpvc release];
}
}
Can Somebody help me in solving the problem?
You're making a new hvc and vpvc each time you run this function.
The first time through, I assume you call removeTabBar:#"Help", it makes a hvc and vpvc and then shows the correct one.
The second time you call it removeTabBar:#"VideoPlayList", you are making a new hvc and vpvc. This means that when you call hvc dismissModalViewController:YES]; you're not removing the one you added before, you're removing the new one that you just made which isn't being displayed at all!
To solve this you need to make your two controllers as properties in your app delegate and create them in the applicationDidFinishLaunching method.
Add these into your app delegate's .h file:
#class MyAppDelegate {
HelpViewController *hvc;
VideoPlaylistViewController *vpvc;
}
#property (nonatomic, retain) HelpViewController *hvc;
#property (nonatomic, retain) VideoPlaylistViewController *vpvc;
#end
and in your app delegate's .m file :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
self.hvc = [[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle:nil] autorelease];
self.vpvc = [[[VideoPlaylistViewController alloc] initWithNibName:#"VideoPlaylistViewController" bundle:nil] autorelease];
...
}
and remove the first two lines in removeTabBar
Memory management is a very important issue in iPhone. So I am asking a very general question.
There are two ways to call a the viewController of another class.
Way 1:
AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:#"AnotherClassView" bundle:nil] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
Way 2:
#import "AnotherClassViewController.h"
#interface ThisClassViewController : UIViewController{
AnotherClassViewController *myViewController;
}
#property (nonatomic, retain) AnotherClassViewController *myViewController;
#end
#implementation ThisClassViewController
#synthesize myViewController;
- (void) pushAnotherViewController{
if(self.myViewController == nil){
AnotherClassViewController *tempViewController = [[AnotherClassViewController alloc] initWithNibName:#"AnotherClassView" bundle:nil];
self.myViewController = tempViewController;
[tempViewController release];
}
[self.navigationController pushViewController:myViewController animated:YES];
}
- (void)dealloc{
self.myViewController = nil;
}
#end
So the obvious question is, which is the best way to call the viewController of other class ?
Way1 or Way2?
Suggestions and comments are openly invited.
Please comment and vote.
Hmm... To keep things simple, why not just:
MyViewController* viewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Way 1 is simpler.
Way 2 lets the first controller keep a reference to the pushed view controller. If you need that reference, then this would be useful.
There is no clear answer here. It depends upon your needs. The general rule, of course, is to make the code as simple as possible, but no simpler.