I have been stuck here.
I have a NSString in Appdelegate. And I have two views called firstview,second view.In first view I have a label and set text from stringvariable which is in Appdelegate .When I click on the button in first view,It goes to the secondview(added the second view to first view as a subview).In second view I have a back button.when I click the back button it is displaying the first view ( Here I am setting the value which is in the appdelegate.And then used this [self.view removeFromSuperview]).The Problem is first view is appearing but label value is not updating.Can any body tell me how to update the Labeltext.Kindly tell me.
Appdelegate.h
#import <UIKit/UIKit.h>
#class FirstView;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *str1;
}
#property (nonatomic,retain) NSString *str1;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FirstView.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize str1;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FirsView alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
self.str1 = #"view1";
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.h
#import <UIKit/UIKit.h>
#import "secondView.h"
#interface FirstView : UIViewController
{
IBOutlet UILabel *btn;
secondView *cont;
}
-(IBAction)gotoView2:(id)sender;
#end
FirstView.m
#import "FirstView.h"
-(IBAction)gotoView2:(id)sender
{
cont = [[secondView alloc] initWithNibName:#"secondView" bundle:nil];
[self.view addSubview:cont.view];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
**SecondView.h**
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController
{
}
-(IBAction)goBack:(id)sender;
#end
SecondView
#import "SecondView.h"
#import "AppDelegate.h"
#implementation SecondView
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.view removeFromSuperView];
}
There is a pattern how to do such things. You should define a protocol like this:
#protocol FirstViewDelegate <NSObject>
- (void)didDismissSecondView;
#end
Your first view should conform to this protocol:
#interface FirstView: UIViewController <FirstViewDelegate> {
...
in its implementation add function didDismissSecondView:
- (void) didDismissSecondView
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
Your second view has to have a property
#interface SecondView : UIViewController
{
id<FirstViewDeledate> delegate;
}
#property (nonatomic, retain) id<FirstViewDeledate> delegate;
When you show your second view from the first view set its delegate to self of first view
in your function:
-(IBAction)gotoView2:(id)sender
{
SecondView *aView = [[SecondView alloc] init] // or whatever
...//other initialization code
aView.delegate = self;
... // show it
}
and before you dismiss the second view:
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.delegate didDismissSecondView];
[self.view removeFromSuperView];
}
And you done.
A bit long, but works.
Related
*iam a beginner in iphone development.
i have one problem in uitextview...wat iam trying to do is dragging the the selected string in uitextview from out of the textview...and drag it to the tabbarcontroller is it possible plz any on help me on this....*this is the code ihave return up to now....plz help me
#
import <UIKit/UIKit.h>
#import "TabViewController.h"
#class TabBarViewController;
#interface TabBarAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
TabBarViewController *txtviewcontroller;
UITabBarController *tabbar;
NSArray *viewcontrollerarray;
}
#property(nonatomic,retain)NSArray *viewcontrollerarray;
#property(nonatomic,strong)UITabBarController *tabbar;
#property(nonatomic,retain)TabBarViewController *txtviewcontroller;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TabBarViewController *viewController;
#end
#import "TabBarAppDelegate.h"
#import "TabViewController.h"
#implementation TabBarAppDelegate
#synthesize txtviewcontroller,tabbar,viewcontrollerarray;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=[UIColor whiteColor];
self.tabbar=[[UITabBarController alloc]init];
txtviewcontroller=[[TabBarViewController alloc]init];
tabbar.delegate=self;
viewcontrollerarray=[[NSArray alloc]initWithObjects:txtviewcontroller, nil];
self.tabbar.viewControllers=viewcontrollerarray;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPhone" bundle:nil];
} else {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
#interface TabBarViewController : UIViewController
{
UITextView *textview;
}
#property(nonatomic,retain)UITextView *textview;
#end
#import "TabViewController.h"
#import "TabBarAppDelegate.h"
#include <QuartzCore/CoreAnimation.h>
#interface TabBarViewController ()
#end
#implementation TabBarViewController
#synthesize textview;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title=#"firstname";
CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
textview = [[UITextView alloc] initWithFrame:textViewFrame];
textview.backgroundColor=[UIColor clearColor];
textview.textColor=[UIColor blackColor];
textview.editable=NO;
NSString *filePath=[[NSBundle mainBundle]pathForResource:#"satyadetails" ofType:#"txt"];
NSString *contentString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
textview.text=contentString;
textview.layer.borderWidth = 3.0f;
textview.layer.borderColor = [[UIColor grayColor] CGColor];
textview.returnKeyType = UIReturnKeyDone;
[self.view addSubview:textview];
enter code here
}
Step 1. Get event when user touches inside textview (You can get that by UITextView's Delegate (startEditing delegate))
Step 2. Add a UILabel on ur view giving position where user touches in textview and giving text as that of textview and giving clear color as backgroundcolor. (Do this inside delegate of textview)
Step 3. Inside touches move of ur view change the positions of ur label dynamically according to touches.
Step 4. When user moves its touches till dropping textview delegate will be called check there if(textview==droppingtextview) then put draggingtextview.text=label.text. and remove label from superview.
I'm trying to change UITextView.text from SecondViewController in ViewController. I can do it if it was NSString type in SecondViewController by doing:
SVC.string = #"test";
The problem is that:
messageBox.text doesn't change
SVC.messageBox.text returns (null)
One of the methods in ViewController.m:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text); // result = (null)
[self presentViewController:SVC animated:YES completion:NULL];
SecondViewController.h:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (nonatomic) IBOutlet UITextView *messageBox;
#end
SecondViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.messageBox.text = #"first";
}
How to get over with this?
Try this,
One of the methods in ViewController.m:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.strMessage= #"changed";
[self presentViewController:SVC animated:YES completion:NULL];
SecondViewController.h:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
IBOutlet UITextView *messageBox;
}
#property (nonatomic) NSString *strMessage;
#end
SecondViewController.m:
#synthesize strMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.messageBox.text = strMessage;
}
You can use delegates to do it as well.
Example:
Viewcontroller2nd.h
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) changeLabel:(NSString*)str;
#end
#interface ViewController2nd : UIViewController{
IBOutlet UIButton *bttn;
id <SecondViewControllerDelegate> delegate;
}
#property (retain) id delegate;
#end
Viewcontrollerone.h
#interface ViewController : UIViewController <SecondViewControllerDelegate>
{
IBOutlet UILabel *lbl;
}
-(IBAction)passdata:(id)sender;
#end
Viewcontrollerone.m
-(void) changeLabel:(NSString*)str{
lbl.text = str;
}
More about delegates:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps...
The problem with your code is your accessing the uicontrols which are not yet created in the memory.
You have to change the order of the execution of your statements like this...
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text);
Set #property (nonatomic) IBOutlet UITextView *messageBox; to #property (nonatomic, strong) IBOutlet UITextView *messageBox; and check if you have connected it with UItextView. And don't reset it in viewDidLoad. Another thing you can try is:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text); // result = (null)
Along with what mentioned above.
just property-synthesize string andf just assign this string to yourTextView.text. For example see bellow..
#interface SecondViewController : UIViewController{
NSString *strMsgText;
}
#property(nonatomic, retain) NSString *strMsgText;
#end
#implementation SecondViewController
#synthesize strMsgText;
after that when you present SecondViewController at that time assign your value like bellow..
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.strMsgText = #"changed";
[SVC.strMsgText retain];
[self presentViewController:SVC animated:YES completion:NULL];
and in viewDidLoad: method of SecondViewController just assign that string to messageBox(TextView), like bellow...
- (void)viewDidLoad
{
messageBox.text = strMsgText;
}
I have two view controllers: view controller and viewcontroller2nd. I have UILabel in one of them and would like to change it when the button( named Go) in the viewcontroller2nd is clicked. I am using delegates and protocols to do it.
The code looks like this:
ViewController.h
#import <UIKit/UIKit.h>
#import "ViewController2nd.h"
#interface ViewController : UIViewController <SecondViewControllerDelegate>
{
IBOutlet UILabel *lbl;
ViewController2nd *secondview;
}
-(IBAction)passdata:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "ViewController2nd.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) changeLabel:(NSString*)str{
lbl.text = str;
}
-(IBAction)passdata:(id)sender{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}
#end
Viewcontroller2nd.h
#import <UIKit/UIKit.h>
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) changeLabel:(NSString*)str;
#end
#interface ViewController2nd : UIViewController{
IBOutlet UIButton *bttn;
id <SecondViewControllerDelegate> delegate;
}
#property (retain) id delegate;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
#end
ViewController2nd.m
#import "ViewController2nd.h"
#import "ViewController.h"
#interface ViewController2nd ()
#end
#implementation ViewController2nd
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)bttnclicked{
[[self delegate] changeLabel:#"Hello"];
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
The passing of the control between the two views is working correctly. However, when i click the go button in the viewcontroller2nd, it doesn't change the value of the label to Hello. What is wrong with the code? Need some guidance.
It seems you never set the delegate.
You can do it in your passData method :
-(IBAction)passdata:(id)sender{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
second.delegate = self;
[self presentViewController:second animated:YES completion:NULL];
}
Hm, try the following in your changeLabel delegate method:
-(void) changeLabel:(NSString*)str{
lbl.text = str;
[lbl setNeedsDisplay];
}
EDIT:
Other than that:
Your label is a IBOutlet, right? Did you connect the label in your xib with the IBOutlet property (i.e. lbl) correctly in interface builder with the files owner
If you want to do this by using delegates then change passdata like:
-(IBAction)passdata:(id)sender
{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[second setDelegate:self];
[self presentViewController:second animated:YES completion:NULL];
}
You can do this without delegates.
Just create a object of ViewController in secondView and synthesize it like:
#import "ViewController.h"
#interface ViewController2nd : UIViewController
{
IBOutlet UIButton *bttn;
ViewController *parent;
}
#property (nonatomic, assign) ViewController *parent;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
#end
and in the passdata change it like
-(IBAction)passdata:(id)sender
{
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
[second setParent:self];
[self presentViewController:second animated:YES completion:NULL];
}
and change the bttnclicked like:
-(IBAction)bttnclicked
{
[parent changeLabel:#"Hello"];
}
I wrote following for you!
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController () <ViewController2Delegate>
#end
#implementation ViewController
-(void)passdata:(NSString *)data
{
self.label.text = data;
}
- (IBAction)buttonClicked:(id)sender {
ViewController2 *v = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
v.delegate = self;
[self presentViewController:v animated:YES completion:nil];
}
#end
#import <UIKit/UIKit.h>
#protocol ViewController2Delegate <NSObject>
- (void)passdata:(NSString*)data;
#end
#interface ViewController2 : UIViewController
#property(assign, nonatomic) id<ViewController2Delegate> delegate;
#end
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)buttonClicked:(id)sender {
[self.delegate passdata:#"hello"];
}
- (IBAction)backButtonClicked:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
You don't need to import the header "ViewController2nd.h" in the both ViewController.h and ViewController.m
You declare the UILabel without property like, #property (strong, nonatomic).
May be, You didn't properly map the UILabel to your .storyboard or .xib file.
You forget to assign the second view controller delegate while calling the controller
ViewController2nd *second = [[ViewController2nd alloc] initWithNibName:nil bundle:nil];
second.delegate = self;
[self presentViewController:second animated:YES completion:NULL];
If anyone is using storyboard for viewcontrollers navigation.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier hasPrefix:#"view_to_capture"]) {
ViewController2nd *destination = segue.destinationViewController;
destination.delegate = self;
}
}
I have a problem with my app (ipad) on iOS4. The Keyboard doesn't work, doesn't display on my webview AFTER the first view (login view).
Appdelagate :
AppDelegate.h
#class RootViewController;
#interface StandardFacileAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;
//[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
return YES;
}
RootViewController :
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewController];
[self.view addSubview:viewController.view];
}
- (void)loadViewController {
ViewController *viewControllertemp = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.viewController = viewControllertemp;
[viewControllertemp release];}
viewController is my login view after request i call the method for change the view (my webview) :
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView{
UIViewController *currentView = (UIViewController*)fromView;
NSLog(#"currentView =>%#",currentView);
if(!ipadTheWebView)
[self loadipadTheWebView];
[currentView.view removeFromSuperview];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
[self.view addSubview:ipadTheWebView.view];
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[ipadTheWebView.webView loadRequest:requestURL];
My webview is ok but if i want search word in google, the keyboard doesn't display (the view scroll) when the textfield have the focus.
What is the problem, this code working on iOS5 but not ios4?
Thanks for your help ;)
Edit :
Thanks Mathieu,
RootViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#class IpadTheWebView;
#interface RootViewController : UIViewController {
ViewController *viewController;
IpadTheWebView *ipadTheWebView;
}
#property (nonatomic,retain) ViewController *viewController;
- (void)loadViewController;
- (void)changeViewToViewController:(UIViewController *)fromView;
#property (nonatomic,retain) IpadTheWebView *ipadTheWebView;
- (void)loadipadTheWebView;
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView;
- (void)keyboardWillShow:(NSNotification *)notification;
- (void)keyboardWillHide:(NSNotification *)note;
#end
My app has an variable called int antalratt, which is the number of correct answers in that view. Now I want to pass that variable to the next view, where I want to get the number of correct answers to be shown! I know how to get an integer to a label text though!
The int antalratt is written in the firstviewcontroller.m, how do I make it "global" so that I can use it in the secondviewcontroller?
Thanks in advance!
make a variable in the public interface of secondviewcontroller.h
#property (nonatomic, strong) NSNumber *correctAnswers;
synthesize it in .m and then pass the value of antalratt in firstviewcontroller with secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];
to secondviewcontroller. then set the labeltext
Method 1:
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
fvc.answer = antalratt;
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
#interface FirstViewController : UIViewController
{
int answer;
}
#property(nonatomic,assign) int answer;
#implementation FirstViewController
#synthesize answer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%d",answer); // //displays answer on log
}
#end
Method 2 (AppDelegate)
AppDelegate
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int antalratt;
}
#property(nonatomic ,assign) int antalratt;
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
delegate.antalratt = antalratt;
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSLog(#"%d",delegate.antalratt); //displays answer on log
}
Method 3 (NSUserDefaults)
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
[[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:#"answer"];
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release]; }
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:#"answer"] intValue];
NSLog(#"%d",ans); //displays answer on log
}
filename *detailViewController = [[filename alloc] initWithNibName:#"filename" bundle:nil];
detailViewController.audio=#"yourData";
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];
Declare in filename.h
NSString *audio;
#property(nonatomic,retain) NSString *audio;
and filename.m
#synthesize audio;
-(void) ViewDidLoad
{
NSLog(#"Audio = %#",audio); // if ur variable is integer declare %d in nslog.
}
thats all
//
view1.h
#interface view1 : UIView{
NSString *passingVariable;
}
#property (nonatomic, strong) NSString *passingVariable;
#end
//
view1.m
#synthsize passingVariable;
#implementation view1
#end
// in another view
view2.m
#import "view1.h"
#implementation view2
-(IBAction)changeview
{
view1 *myview = [[view1 alloc]init];
myview.passingVariable = [NSString stringWithString:#"Hello Variable"];
[self.navigationController pushViewController:myview animated:YES];
}
#end