RootViewController - iPhone - iphone

I am new to iPhone programming and trying to grasp the concept of RootViewController.
Scenario:
I have 3 views
RootViewController - Parent View
SportsViewController - Sub View 1
CricketViewController - Sub View 2
Both the subview have to be in FullScreenMode, so tab-bar or navigation bar cannot be used.
At first, Sub View 1 is loaded which is having some content and a DONE button on it.
Once user press DONE button then Sub View 1 has to be unloaded and RootViewController should load the Sub View 2.
Query
I have successfully displayed SubView 1 and when user taps on DONE then I can unload it. But I did n't get how should I notify the RootViewController from Sub View 1 that Sub View 1 has unloaded and now it should load the Sub View 2?
Thanks in Advance
Paras Mendiratta

I think the easiest solution here is to use UINavigationController and just hide the navigation bar. You can use -setNavigationBarHidden:animated: to hide (or show) the nav bar.

one way is to implement a method like - (void)loadSecondView which does all u want to do when first view is unloaded. and then int doneButtonClicked-method u call this method like this: [super loadSecondView]; and also remove first view from superview.

I assume that the screen with the "Done" button is some kind of login screen. You actually do not need all the view controllers you have defined to do what you want.
Instead you could do like this:
At application launch set CricketViewController as the root view controller.
Immediately let CricketViewController present `SportsViewController" as a modal view controller, no animation.
As far as the user is concerned the sports view controller is the starting point.
Dismiss the modal view controller on the Done button tap, giving the user the illusion of going to the next view.

view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
view1 = navigationController1;
[window addSubview:view1.view];
[window makeKeyAndVisible];
This is general idea so please change according to your problem.

Here is my code where I tried to use delegate pattern.
The problem is the sub view 1 (videoPlayer) is not able to call the delegate methods. :(
ViewSwitcher.h - Root Controller
#class VideoPlayer; //Sub View 1
#class LandingPage; //Sub View 2
#protocol viewSwitcherDelegate
-(void)notifyViewSwitcher;
#end
#interface ViewSwitcher : UIViewController
{
id <viewSwitcherDelegate> delegate;
}
#property (nonatomic, retain) VideoPlayer *videoPlayer;
#property (nonatomic, retain) LandingPage *landingPage;
#property(assign) id <viewSwitcherDelegate> delegate;
-(void)loadSecondView;
-(void)delegateSetInSubView;
#end
ViewSwitcher.m - Implementation
#synthesize videoPlayer;
#synthesize landingPage;
//#synthesize delegate;
// 1.4 -> Declare the delegate constructor
- (id <viewSwitcherDelegate>)delegate
{
return delegate;
}
// 1.5 -> Declare the setDelegate method
- (void)setDelegate:(id <viewSwitcherDelegate>)v
{
delegate = v;
}
- (void)viewDidLoad
{
VideoPlayer *videoController = [[VideoPlayer alloc] initWithNibName:#"VideoPlayer" bundle:nil];
self.videoPlayer = videoController;
[self.view insertSubview:videoController.view atIndex:0];
[videoController release];
[super viewDidLoad];
}
-(void)loadSecondView
{
NSLog(#"Call for loading 2nd View");
}
VideoPlayer.h - SubView 1 (Movie Player)
#interface VideoPlayer : UIViewController <viewSwitcherDelegate>
{
ViewSwitcher *viewSwitcher;
MPMoviePlayerController *videoController;
}
#end
VideoPlayer.m - implementation
-(void)notifyViewSwitcher
{
NSLog(#"notifyViewSwitcher called.");
//Attempted to call the loadSecondView of ViewSwitcher
I tried calling delegate's method but in log nothing is printed.
[viewSwitcher loadSecondView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Setting the delegate
viewSwitcher.delegate = self;
return self;
}
- (void)viewDidLoad
{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"bumper.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
videoController = [[MPMoviePlayerController alloc] initWithContentURL:url];
videoController.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:videoController.view];
videoController.view.frame = CGRectMake(0, 0, 480, 320);
videoController.fullscreen = YES;
// Remove the status bar from this view.
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:UIStatusBarAnimationFade];
// TODO: This observer needs to be removed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateChange:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoController];
// Play the video.
[videoController play];
[super viewDidLoad];
}
// Receives notification once movie is finished.
-(void)playbackStateChange:(NSNotification*)notification
{
// TODO: Switch the view.
NSLog(#"Notification = %#", notification);
[self notifyViewSwitcher];
}
Here is the LOG:-
2011-08-03 02:44:47.333 My Video Player[24016:207] Notification = NSConcreteNotification 0x5768280 {name = MPMoviePlayerPlaybackDidFinishNotification; object = <MPMoviePlayerController: 0x5740940>; userInfo = {
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 0;
}}
2011-08-03 02:44:47.337 My Video Player[24016:207] notifyViewSwitcher called.

Related

UIImagePickerControllerCameraDeviceFront works every other time

This question is very similar to an existing question asked here UIImagePickerControllerCameraDeviceFront only works every other time I tried the solution presented but it didn't work for me
I have a simplest of a project with two view controllers. In the blue one I am displaying a small UIView with a UIImagePickerController in it. NOTE: I am displaying front facing camera when app is launched.
I hit the next button and go to orange view controller and when I hit the back button and come back to blue view controller the UIImagePickerController flips from Front to rear. I guess the reason is that it thinks its busy and moves to the rear cam. If I keep moving back and forth between the view controllers the camera keeps flipping front, back, front, back, front, back...
Here is my code and screenshots, what am I doing wrong?
In my *.h
#import <UIKit/UIKit.h>
#interface v1ViewController : UIViewController <UIImagePickerControllerDelegate>
{
UIImagePickerController *picpicker;
UIView *controllerView;
}
#property (nonatomic, retain) UIImagePickerController *picpicker;
#property (nonatomic, retain) UIView *controllerView;
#end
In my *.m file (This code is only used when blue colored view controller is displayed)
#import "v1ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#implementation v1ViewController
#synthesize picpicker;
#synthesize controllerView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
controllerView = picpicker.view;
[controllerView setFrame:CGRectMake(35, 31, 250, 250)];
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
[self.view addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
You are dismissing the controller in both the viewDidDisappear and viewWillDisappear methods.
That could be the cause of your problem.
Although I do not have a device with a camera available right now to verify this, it seems that you're not dismissing the pickerview controller correctly. The documentation states that you should call dismissModalViewControllerAnimated: on the parent controller in order to dismiss the picker (though, calls to presented controllers will propagate to presenters - so this is not the problem), but in your case you're not displaying the controller modally in the first place so it will not work.
What I would try in this case is to release the picker instead (if not under ARC) and set it to nil (instead of calling [picpicker dismissModalViewControllerAnimated:YES];).
PS. In fact, it seems that there is a bigger problem with your design. Since each button is set to present the other party modally you are not dismissing any of the controllers ever. The controllers just keep stacking on each other. You should either consider to embed them in a navigation controller and have it handle the hierarchy or just set dismissModalViewControllerAnimated: (dismissViewControllerAnimated:completion: on iOS5+) as the action of the second controller's button instead of a modal segue.
This is a very simple issue. I don't know why this happens exactly, but it seems that UIImagePickerController was designed to recreated each time it's needed instead of keeping any reference to it, which seems logical if you think about it. Basically, you need to recreate and reconfigure your picker each time. Below I've pasted some code to give an image of what I mean.
Simple solution:
- (UIImagePickerController *)loadImagePicker {
UIImagePickerController *picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
return picpicker;
}
and in:
-(void)viewWillAppear:(BOOL)animated{
if(!self.picpicker){
self.picpicker = [self loadImagePicker];
[self.view addSubview: self.picpicker];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.picpicker removeFromSuperview];
self.picpicker = nil;
}

how to get textfield value of second modal view to the textfield of first modal view [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing Data between View Controllers
I have 2 modal views. The first modal view is used to edit quantity and price; the second modal view is used when we click the price textfield of first modal view in order to give give reason why we change the price and we can put new price in price textfield of modal view. I want the price in first modal view change when I set the price in second modal view. How to catch the value of second modal view to put in first modal view ?
Use NSNotification center
You have to addobserver event in First Modalview
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reload:) name:#"refresh" object:nil];
}
- (void)reload:(NSNotification *)notification {
textfield.text= [[notification userInfo] valueForKey:#"price"] ;
}
In second modalview you have to post notification after you complete edit
(pass your textfield value)
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:#"333" forKey:#"price"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"refresh" object:nil userInfo:userInfo]
;
Finally remove observer
-
(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"refresh" object:nil];
}
You can used Singleton just to save some data on that class
The following simple steps will make you able to do this.
In the first Modal ViewController, you can declare a function like
- (void) setUpdatedValueWithValue: (NSString *) newValue
{
self.objTextField.text = newValue;
}
Declare this function on the header file too, so that we can access it from the other class.
In the second Modal ViewController
SecondViewController.h
#interface SecondViewController : UIViewController
{
id objFirstViewController;
}
#property (nonatomic, retain) id objFirstViewController;
#end
SecondViewController.m
#implementation SecondViewController
#synthesize objFirstViewController;
#end
Before you present the SecondViewController pass the object of FirstViewController to SecondViewController like,
- (void) presentSecondViewController
{
SecondViewController *objSecondViewController = [[SecondViewController alloc] init];
objSecondViewController.objFirstViewController = self;
[self presentModalViewController: objSecondViewController animated: YES];
[objSecondViewController release];
objSecondViewController = nil;
}
Then, in the function you are calling to dismiss the SecondViewController after the value edit you can do like,
- (void) finishEdit
{
if([objFirstViewController respondsToSelector: #selector(setUpdatedValueWithValue:)])
{
[objFirstViewController performSelector: #selector(setUpdatedValueWithValue:) withObject: editedTextView.text];
}
[self dismissModalViewControllerAnimated: YES];
}
Hope this helps.
set you object with your keyword
[[NSUserDefaults standardUserDefaults]setObject:#"value of second modal view" forKey:#"keyName"];
than get that object in first modal view
NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:#"keyName"];

Set Delegate methods on child view controll

I am building an application which should allow the user to scroll through the images. Since I have many images (downloaded from the web), what i doing is I have UP and down button on the parent view, in addition i have the scroll view. Based on the selected option (up or down), I add the ImageClass ( created a class which extends UIViewController) view to the scroll view.
Now, on the selected view the user can mark a point or do any stuff.
The question is how from the parent view I can call methods of the Uiviewcontroller. I know how I can set the delegate methods but what I want is that parent controller can call any a method say redraw method which would redraw the entire view.
Code:
-(IBAction) down:(id) sender{
[scrollView2 removeFromSuperview];
}
-(IBAction) down :(id) sender {
[scrollView2 removeFromSuperview];
if(scrollView2 == nil)
scrollView2 = [[UIScrollView alloc] init];
[self.view addSubview:scrollView2];
[self.view sendSubviewToBack:scrollView2];
[scrollView2 setBackgroundColor:[UIColor blackColor]];
[scrollView2 setCanCancelContentTouches:NO];
scrollView2.clipsToBounds = YES;
// default is NO, we want to restrict drawing within our scrollview
scrollView2.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// CSImageView is a class which of the type UIViewController
imageVie = [[CSImageView alloc] init];
[scrollView2 addSubview:imageVie.view];
[scrollView2 setContentSize:CGSizeMake(1500,1500)];
[scrollView2 setScrollEnabled:YES];
[imageView release];
}
Now, from the parent view controller I want to call say:
imageVie.redraw(); method
Code for CSImageView
#interface CSImageView : UIViewController {
NSNumber *imageId ;
}
#property (nonatomic, retain) NSNumber *venueId;
-(void) redraw;
#end
#implementation CSImageView
#synthesize imageId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) redraw {
NSLog(#"I am ehre in the test function");
}
#end
Can you please help me on the same. I am not able to call the redraw method. Any input would be appreciated.
First, CSImageView should probably be a subclass of UIView, not UIViewController. You should keep a reference to your instance (i.e. as an instance variable or synthesized property) so you can access it from methods other than - down:.
Wow.
I can't say I follow what you've coded, but here's how you do it.
During this part...add a tag to the view
// CSImageView is a class type UIViewController
imageVie = [[CSImageView alloc] init];
imageVie.tag = 99 // Any number. Preferably a constant
[scrollView2 addSubview:imageVie.view];
...
Then on the parent
view controller. You can ...
-(IBAction) down:(id) sender {
CSImageView *view = [scrollView2 viewWithTag:99];
[view redraw];
... }
Something like that.

How to create new view w/ webview from push of button

UPDATED QUESTION:
Hello , so I was able to create the modal view curl up overlay for this use. However I now have the issue that I can click on the button1 and pull up second view with the url1 on webView1, but when I click button2 I receive the display of url1 on webview also. below is the code can you help me out on what I'm doing wrong?
FourthViewController.h has the following code:
'
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController {
UIButton *openBlogButton, *openFbButton, *openYelpButton;
}
#property (nonatomic, retain) IBOutlet UIButton *openBlogButton, *openFbButton, *openYelpButton;
-(IBAction)openBlog:(id)sender;
-(IBAction)openFacebook:(id)sender;
-(IBAction)openYelp:(id)sender;
#end
'
FourthViewController.m has the following code:
'
#import "FourthViewController.h"
#import "FourthBlogWebViewController.h"
#import "FourthFBWebViewController.h"
#implementation FourthViewController
#synthesize openBlogButton, openFbButton, openYelpButton;
-(IBAction)openBlog:(id)sender {
FourthBlogWebViewController *sampleView = [[[FourthBlogWebViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
//THIS IS WHERE I THINK THE ISSUE IS, WHEN I CLICK openFacebook it should lead to the fb URL, it seems as if I should be able to list all possible urls on the first fourthwebviewcontroller, but i was not able to be successful so I tried making a second webview controller to feed it this url but this failed...lol. thnx for any input.
-(IBAction)openFacebook:(id)sender {
FourthFBWebViewController *sampleView = [[[FourthFBWebViewController alloc] init] autorelease];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
-(IBAction)openYelp {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.yelp.com"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[openBlogButton release];
[openFbButton release];
[openYelpButton release];
[super dealloc];
}
#end
'
FourthBlogWevViewController.h
'
#import <UIKit/UIKit.h>
#interface FourthBlogWebViewController : UIViewController {
UIButton *dismissViewButton;
IBOutlet UIWebView *webViewFB;
}
#property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;
#property (nonatomic, retain) UIWebView *webViewFB;
- (IBAction)dismissView:(id)sender;
#end'
FourthBlogWebViewController.m code:
'#import "FourthBlogWebViewController.h"
#implementation FourthBlogWebViewController
#synthesize dismissViewButton, webViewFB;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
}
-(IBAction)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.facebook.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webViewFB loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[dismissViewButton release];
[webViewFB release];
[super dealloc];
}
#end
Where or how would I add the second url and have it distinguis on click of other buttons. Thank you in advance for the ongoing help.
Original Question:
Hello,
So first app and I'm trying to make some basic function use of buttons, Im doing this in a tab bar application. On my fourth tab view a view opens with 3 buttons, on the click of the button I am trying to get a webpage to load within the app (i was able to successfully make the webpages open via safari on button touchup inside click, but that is not how I want it to load. So I edited the way it loads and on push of button the website is now opening within the webView. However I want to create this webview on a new view rather then on the same view where the 3 buttons are, but I'm not clear on how to do this. Any input would be greatly appreciated (I apologize beforehand since this seems as an easy task, but haven't figured it out yet, I did some searching before asking, but was unable to find anything that showed how to do this on push of different button to load webview on a new view within the same XIB.
Thanks.
Below is the code that I have for the load of webview on push of button.
On fourthviewcontroller.h
#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController {
IBOutlet UIWebView *webView;
}
-(IBAction)openWebsite:(id)sender;
-(IBAction)openButton2;
-(IBAction)openButton3;
#property (nonatomic, retain) UIWebView *webView;
#end
On fourthviewcontroller.m
#import "FourthViewController.h"
#implementation FourthViewController
#synthesize webView;
-(IBAction)openWebsite:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL.com"]]];
}
-(IBAction)openButton2:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL2.com"]]];
}
-(IBAction)openButton3:(id)sender{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.URL3.com"]]];
}
Basically I'm trying to get different buttons to push to the same new view but the only difference is that i want the uiwebview to display a different URL depending on the button click that bring the new view.
As NWCoder mention, it depends how you like the new webview to be shown.
Display in UINavigationController-
Make your fourth tab a uinavigationcontroller and set the 3 button view controller as the top most view controller.
Create a new UIViewController that encapsulate the UIWebView.
When user click on the open webview button, create and push the uiviewcontroller that contains the uiwebview
Display in modal view:
Create a new UIViewController that encapsulate the UIWebView.
When user click on the open webview button, create and push the webview uiviewcontroller by calling the fourth's tab uiviewcontroller presentModalViewController:animated.

insertSubview: AtIndex: does not work

I went through the material regarding this issue but still can't figure it out completely.
The main issue is that i am using this method but the view does not show...
The app is built out of two view - the main one (i added it as a subview to the main nib) -
Has of a text field where i write some text.
Has a button that when pressed, the action method loads a second view that has a label with some text in it.
the source code of the first viewController:
#import <UIKit/UIKit.h>
#class TxtRunnerViewController;
#interface Itai_s_text_app_take_2ViewController : UIViewController {
int sliderSpeed;
IBOutlet UITextField *textInput;
TxtRunnerViewController *trvc;
}
#property (nonatomic, retain) IBOutlet UITextField *textInput;
#property (retain, nonatomic) TxtRunnerViewController *trvc;
- (IBAction)sliderChanged:(id)sender;//speed of text show changed
- (IBAction)textFieldDoneEditing:(id)sender;//dor 'DONE' on keyboard
- (IBAction)backgroundTap:(id)sender;//for handling tapping on background
- (IBAction)textEmButtonPressed:(id)sender;
#end
the .m of the first ViewController:
#import "Itai_s_text_app_take_2ViewController.h"
#import "TxtRunnerViewController.h"
#implementation Itai_s_text_app_take_2ViewController
#synthesize textInput;
#synthesize trvc;
- (IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
sliderSpeed = (int)(slider.value + 0.5f);//setting the speed determinned by the usr in slider
NSLog(#"Slider value is %#", sliderSpeed);
}
- (IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
NSLog(#"our text input is %#", textInput.text);
}
- (IBAction)backgroundTap:(id)sender
{
[textInput resignFirstResponder];
}
- (IBAction)textEmButtonPressed:(id)sender
{
NSLog(#"button pressed");
if ([textInput.text length]==0)
{
NSString *msg = nil;
msg = #"Write text to transmit";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Forgot something?"
message:msg
delegate:self
cancelButtonTitle:#"Back"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
else { //init
NSLog(#"HI!");
if (self.trvc == nil)
{
NSLog(#"if accepted");
TxtRunnerViewController *tr = [[TxtRunnerViewController alloc] initWithNibName:#"TxtRunner"
bundle:nil];
self.trvc = tr;
[tr release];
}
[self.view removeFromSuperview];
[self.view insertSubview:trvc.view atIndex:0];
NSLog(#"InsertAtIndex was operated...!");
//[self.view addSubview:tvc.view];
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
if(self.trvc.view.superview == nil)
self.trvc = nil;
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[trvc release];
[super dealloc];
}
The second View is simply empty (has a label added to it in it's nib file):
#import
#interface TxtRunnerViewController : UIViewController {
}
#end
It's .m file is the default one, added no code to it.
When i press the button in the first view - it's view disappears but the second view does not appear instead, only a blank white view appears, nothing on it.
Doe
If you push the TxtRunnerViewController it will completely hide the first one, but it still exist in background. (without removeFromSuperview)
TxtRunnerViewController *vw = [[TxtRunnerViewController alloc]initWithNibName:#"TxtRunner" bundle:nil];
[self.view pushViewController:vw.view animated:NO];
[vw release];
To remove the first view it's a bit more difficult. You would need to implement a delegate in RootViewController which will be fired after clicking the done Button of the first view. Then in your RootViewController some code is executed which displays the second view and removes the first view.
For a delegate sample open the Utility Application Sample when creating a new project and figure out how it works or search some examples online.
The line:
[self.view removeFromSuperview];
Removes the view from its super
then the call
[self.view insertSubview:trvc.view atIndex:0];
Adds trvc to it - but that view has been removed and not visible anymore.
You need to add trvc to the superview.