I am creating a code in which i want to pass integer value for one uiview to another view.In another uiview that integer value so as text of label.How make code for that?
Take this in .h file in SecondViewController
int value;
Make below function in SecondViewController
-(void)setValue:(int)number{
value=number;
}
Now In First view controller do like this:
ParentViewController *objSecond = [[ParentViewController] initwithNibName:#"parentView.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
Now, In secondViewController viewWillAppear Method write this.
-(void)viewWillAppear:(BOOL)animated{
myValue = value;
}
Please check spelling mistakes as I hand written this. Hope this help.
If you are not using navigationContoller then you can do something like this.
SecondViewControler *objSecond = [[SecondViewController] initwithNibName:#"secondview.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
You can pass it as integer value itself as u do in c or in any other language.
In the second view, u can convert it to string with [NSString stringWithFormat:#"%d",intVal];
In second view, declare a NSUInteger variable , say NSUInteger _intVal.
In .h file, declare a method like
-(void)setIntVal:(NSUInteger)inVal;
In .m file,
-(void)setIntVal:(NSUInteger)inVal
{
_intVal = inVal;
}
Now u can use the _intVal in the second view.
declare a varable in .h file
NSInteger tempValue;
declare a method like that:
- (void)SetValue:(NSInteger)value {
tempValue=value;
}
- (void)GetValue{
return tempValue;
}
when you set it, you use:
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app SetValue:xxxx]
when you need it, use:
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
xxxx = [app GetValue];
When you are going from view1 to view2 then do like this.
Take one variable in view2 and set the property as
#property (nonatomic,readwrite) int val; //this is in the view2 .h file
in view2 .m file do this. #synthesize val;
Then when you are adding the view2 as subview to view1 then
view2object.val=someval;// like 1,2,3....
Related
I have 2 view controller:
Index
Second
In Index I have 2 textfield and button.
I fill this text field and press "next button" to second view, I need to "show" this two text field, by using Array (later I will send JSON data).
I Tried this:
Declare an #property in class Second as textArray. And then while pushing to Second from Index, you need to set this property.
In Second.h file,
#property(nonatomic, strong) NSArray *textArray;
In Index.m file,
Second *aSecond = [[Second alloc] init];
aSecond.textArray = #[textField1.text, textField2.text];
//write code to push from Index to Second
Now in class Second, you can use it as aSecond.textArray[0] and aSecond.textArray[1]
But if i switch page (click next) array has been nulled.
For switch page i use this:
SecondPage *SecondPage = [self.storyboard instantiateInitialViewController];
SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
[self SecondPage animated:YES];
How I can best realize? Please help! Best regards
Ivan.
Write your own designated initializer for second view controller something like.
-(id)initWithTextArray:(NSArray *)_textarray;
Now alloc init the second view controller
Second *aSecond = [[Second alloc] initWithTextArray:array];
in the viewDidLoad method of second view controller use your textarray to populate your views.
You need to do it like this if you are using it via storyboard,
SecondPage *aSecondPage = [self.storyboard instantiateInitialViewController];
aSecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
aSecond.textArray = #[textField1.text, textField2.text];
[self aSecondPage animated:YES];
When you do this aSecond = [[Second alloc] init]; and then do aSecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];, it is creating a separate instance and not the same instances. You were setting this array on the first one and expecting it to be set on the second instance.
Hey had you tried for 'Global variable & Array with Delegate Method'. It'll not release till you'll remove. It'll be global till the application instance.
In Delegate Class,
.h File
NSMutableArray *obj;
.m file,
Synthesize & Alloc - init array.
Then,
In ViewContoller .m file
AppDelegate *app;
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
then,
[app.obj addObject: text1.text];
In Second .m file, you can use it.
NSLog(#" Data : %#", app.obj);
Keep in mind, whenever you want this array, you should initialize 'app' object of 'Delegate' class.
It'll work & stores data globally.
Thanks.
I can think of two methods:
1) Either add a method in Second and after initializing call it and pass the value as an argument to a method. so in Second.m -(void) push :(NSArray *)array{} pass it with "&" before your NSArray name.
2) Create a getter in First. Since its NS, you have to use assign so in First.h you put #property(nonatomic, assign) NSArray *array1;
In First.m you add the #synthesize array1;
and call it with
self.array1 = [#"t", #"e", #"s", #"t"];
And then pass the reference to First class in Second and create a variable in Second.m and get it
NSArray *array1 = first.array1;
You can write your own getter, but not reasonable.
SecondPage *SecondPage = [self.storyboard instantiateInitialViewController];
SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
[self SecondPage animated:YES];
1) Your variable name and your class name are the same
2) I'm not sure XCode accepted that you wrote [self SecondPage animated:YES];
Finally, did you alloc your array ?
EDIT;
Oh and :
aSecond.textArray = #[textField1.text, textField2.text];
You should write:
[aSecond.textArray addObject:textField1.text]; // using NSMutableArray
There are lot of ways to transfer the value from one controller to an other, but for you I think this will be the most simple ever. Use NSUserDefaults
First Easy Ways
in first Controller
[[NSUserDefaults standardUserDefaults] setObject:#[textField1.text, textField2.text] forKey:#"listOfData"];
in 2nd Controller
NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:#"listOfData"];
2nd Easy ways is
DetailVC *detailVC = [self.storyboard instantiateInitialViewController];
detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailVC"];
detailVC.textArray = #[textField1.text, textField2.text];
I have a method "someMethod" declared in OneViewController.h
#interface OneViewController
{
UIView *tempView;
..
}
-(void) someMethod ;
#end
and implemented in OneViewController.m file
#implementation OneViewController
-(void) someMethod
{
tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 200, 250)];
tempView.backgroundColor = [UIColor yellowColor];
if([[self.view subviews] containsObject:tempView])
[tempView removeFromSuperView];
else
[self.view addsubview:tempView];
}
I want to call someMethod when present at different viewController - secondViewController
(something like [OneViewController someMethod]), So that when I get back to OneViewController I can see the changes made by someMethod.
Do I need to use appDelegate methods?
I have tried following but it doesn't work.
neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];
Thanks for any help in advance..
In the SecondViewController, declare a reference for OneViewController class. You can have assign property. Set the reference before you move to SecondViewController. Now with the reference, you can call the instance method [_oneView someMethod].
Edit:
Declare
OneViewController *_oneView;
Also add the assign property,
#property(nonatomic,assign) OneViewController *_oneView;
Synthesize the variable in .m file.
While showing the SecondViewController from OneViewController, just add the following line.
secondView._oneView = self;
sometimes calling a method directlry creating [classObject methodName] does not refelect the changes in views. Like if you want to change a UIScrollView property from scrollEnble = NO; to scrollEnable = YES;, it does not refelect.
You should use singleton of UIApplication.
Suppose you want to call ViewController1's method - (void)myMethod in ViewController2 then here are the steps:
In you AppDelegate import ViewController1 and create its object *vc. Delare property #property (strong, nonatomic) ViewController1 *vc;. synthesize also.
Now come to Viewcontroller1 class. Import AppDelegate.h in you Viewcontroller1's and in viewDidLoad write like this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.vc1 = self;
Go to your ViewController2.h and import AppDelegate.h
Go to the line where you want to call ViewController2's method and write like this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate vc1] myMethod]; // to allow scrolling
Ideally, you should create a protocol and delegate methods to accomplish what you are looking for.
Create a protocol, implement it in secondViewController and set the protocol delegate to firstViewController and then use the delegate methods for invoking the relevant methods in secondViewController
I hope it works for you..!!
One way of doing it is to change the declaration to +(void) someMethod ; in your OneViewController.h and change the minus to a plus correspondingly in the implementation file. This will make it a class-method and not an instance method. Then in your SecondViewController.m file, make sure to put #class OneViewController; before the implementation declaration; then you can call [OneViewController someMethod] and it should execute. Cheers!
hi everyone i am new to iphone development.
Actually i am trying with some sample app where i have a textField in the Viewcontroller and a Button,when i enter a string in the textfield and press the button it should display the same string in NextView.so can anyone help me out in doing this.
i worked with transition between one view to another view,but i need copy string from Viewcontroller1 to NextView
#ViewController1
-(IBAction)next
{
NextView *Nview = [[NextView alloc]initWithNibName:#"NextView" bundle:nil];
[self.view addSubview:Nview.view];
}
Set an ivar in your NextView called "newString" for example, then pass a string to that ivar form your first controller.
For Example, not tested (and this is one of many ways you can do this):
FirstView
NextView * next = [[NextView alloc] initWithNewString: myTextField.text];
[self.navigationController presentModalViewController: next animated: YES];
NextView
#synthesize newString
-(id)initWithNewString:(NSString*)someString
{
newString = someString;
return self;
}
Then throughout your NextView, just call upon newString wherever you want to get the value of the previous views textField.
-(IBAction)next
{
NextView *Nview = [[NextView alloc]initWithNibName:#"NextView" bundle:nil];
Nview.string_Object=string_to_copy;//declare string_Object in NextView.h
[self.view addSubview:Nview.view];
}
Try this:
In your first view while naviagting:
NextView *nav = [[NextView alloc] init];
nav.textFieldValue = textField.text;
[self.navigationController pushViewController:nav animated:YES];
And in your NextView's .h file create a property:
#property(nonatomic, retain) NSString *textFieldValue;
And in .m file synthesize it like:
#synthesize textFieldValue;
Now you can use textFieldValue in NextView class
P.S: Don't forget to release it :)
As you are a beginner, this will be a good guidance for you to understand the exact flow and it will make you learn how to pass values from one class to another.
Here is an approach (haven't tested, but should work)
Give your textField a tag value like
textField.tag = 1;
and in NextView access it like
UITextField *parentViewTextField = (UITextField*)[self.superview viewWithTag:1]
I want to know how to pass values between views in popViewControllerAnimated .
Here is my scenario:
I have a view which contains tableview on selecting the cell we go to another view where i need to enter value in textbox and i click a button to go back to the previous view where i need to display the textbox value in the table view cell.
How can i do this ?
This what i have done:
NewContact *nc = [[NewContact alloc] initWithNibName:#"NewContact" bundle:nil];
// ...
// Pass the selected object to the new view controller.
nc.name=[firstName text];
//[self.navigationController pushViewController:nc animated:YES];
[self.navigationController popViewControllerAnimated:YES];
[nc release];
In ViewControllerB, declare delegate and set action for popViewControllerAnimated:
#interface ViewControllerB : UIViewController {
id delegate;
}
#property (nonatomic, retain) id delegate;
#synthesize delegate;
- (id) init ... {
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(didBack:)] autorelease];
}
- (void) didBack:(id)sender {
if ([delegate respondsToSelector:#selector(setProperty:)]) {
[delegate setProperty:property];
}
[self.navigationController popViewControllerAnimated:YES];
}
In ViewControllerA, provide a function to set the local property and set delegate:
ViewControllerB controllerB = [[ViewControllerB alloc] init...];
[controllerB setDelegate:self];
[self.navigationController pushViewController:controllerB animated:YES];
[controllerB release];
You need to store value in one of the global variable for example you can declare in appDelegate file. See this post for that
If you are using UITextField then you can store value in above variable from UITextField in below delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
Hope this help.
You could use NSNotificationCenter for this, passing an object along with the call.
Howto: Send and receive messages through NSNotificationCenter in Objective-C?
Based on your requirement in your comment,
Say you navigate from ViewControllerA instance to ViewControllerB instance and you wish to call ViewControllerA's methods, you can do it like this,
ViewControllerA * viewControllerA = (ViewControllerA *)self.parentViewController.
[viewController methodToCall];
To use this to address the requirement in the question, you can use a property.
viewControllerA.name = firstName.text; // Bits from your code snippet
However in a table view I would expect you to have a mutable array powering the data source. You can refer to it, assuming that it is an property.
[viewControllerA.dataSourceArray addObject:firstName.text];
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];
}