I have come across a strange behavior...
In class A, at the viewDidLoad method I do:
b = [[B alloc] initWithNibName:#"B" bundle:nil]; //init the B class (declared as B* b)
[b setText:#"ABCDDEEE"]; //change the text of a UITextView in b
note that b's view is not shown until a button is pressed. However, when I press it and go to b's view, the text of the UITextView is still the "lorem ipsun" text
Once b's view is shown once, I can change the text.
Anyone know this issue and how to solve it??
That is expected behaviour. initWithNibNamedoes not guarantee complete initialization before viewDidLoad is called.
Create an NSString #property in B. Set that property when you load b--not the .text property of a UITextView, but an NSString data object in the view controller. Then in B's -(void)viewDidLoad, set the UITextView's text property with the string you set.
NIB elements don't necessarily exist when the parent view controller first instantiates a new view controller, but you can talk to data fields and then load that data into the view hierarchy members in -(void)viewDidLoad.
Related
I am having a trouble on exporting text data. I have a view contains over 3 different uitextview that allow user to type in data. Then i want to export these texts from the 3 different uitextview (e.g. textview1, textview2 and textview3) into a new uiviewcontroller that contains only one uitextview. I also want the textview 1-3 are placed in order. So that the new uitextview will have three different sections in the order of 1-3. I am using PrepareFroSegue method. How should i code this? Many thanks!
What you want to do is concatenate (combine) the strings into one NSString that will be sent into the next view. You can do this in your PrepareForSegue. Put this in your prepareforsegue but edit the code so it works with your code.
NSString *concatenatedString = [NSString stringWithFormat:#"%# %# %#", textview1.text, textview2.text, textview3.text];
YourViewController *dV = [segue destinationViewController];
dV.stringToStoreValue = concatenatedString;
The first line I concatenate the string
The second line I get the destination view controller from the segue
The third line I set the text to a NSString #property in the destination view controller. You must then in the view did load of the destination view controller set
yourcombinedtextfield.text = stringToStoreValue;
I do not directly set the value of the text field in the prepareForSegue because if the view has not loaded yet then the I would be setting nothing and not see the string on the next page in the textfield!
You're trying to communicate between ViewControllers this answer looks like a good starting point for understanding how this is done..
UPDATE :
newController.textView.text = [NSString stringWithFormat:#"%# %# %#", textView1.text, textView2.text, textView3.text];
okay i have a tabbed view, so two view controllers. in one view controller i have a segmented control and to know which segment was selected i have an action connected to it. like this:
-(IBAction)selectAngle:(id)sender{
clickedSegment = [myAngleType selectedSegmentIndex];
}
here clickedSegment is an integer which i have exposed as a property in my header. now i want to use the value of this clickedSegment in my other view controller but whenever i create an instance of that VC(segmented controller one) and try to use clickedSegment, i always get 0 as the value no matter if i selected other segment 1 or 2. its always zero. where am i going wrong?
How is your second view controller going to know about any property in the first view controller? This is impossible, unless you explicitly get a reference to it.
// in second view controller
FirstViewController *firstVC =
(FirstViewController*)[self.tabBarController.viewControllers objectAtIndex:0];
NSLog(#"clicked segment: %d", firstVC.clickedSegment); // assuming int
You are not using the synthesized setter correctly, you should set it like this:
-(IBAction)selectAngle:(id)sender{
self.clickedSegment = [myAngleType selectedSegmentIndex];
}
I have a TabBarController with two tabs; first is InputViewController and second one is TableviewController.
Input view controller has two text fields when I enter the text, after that I want to receive that text filed value in to TableviewController (in the next tab). It does not receive the value.
Try to use this concept.....
Declare two variables in Second tab that is Tableviewcotroller..
For exa..
NSString *strname;
NSString *straddr;
#property and #synthesize both...
Now, in your *InputViewCotroller*tab, create and initialize object of TableViewcotroller, using this object, access these varibles. Like, you want to get values of textfields.
Tableviewcotroller *objTable = ........
objTable.strname = self.textName.text;
objTable.straddr = self.txtAddr.text;
Implement this concept in your project...Hope this helps you...:)
What ever value you have in input view controller like
NSString*text1=textbox1.text;
NSString*text2=textbox2.tex;
In the method where you are moving to next view you should pass the value to the table view controller like this
DetailViewController*tblViewController=[[DetailViewController alloc]init];
NSString*textValue1=text1;
tblViewController.textValue1=textValue;
[self.navigationController pushViewController:tblViewController];
you should alos have textValue NSString in tableView controller also to assign value
i declared an int value as my imageIndexForSend through the following code in myView class.
int imageIndexForSend;
#property int imageIndexForSend;
#synthesize imageIndexForSend;
after on a button click i am displaying a popover which is PopOver calss.
there is table view with multiple indexes in popover class.when i click on any row in PopOver class table it set myView class imageIndexForSend as
In PopOver
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
myView *obj = [[myView alloc] init];
[obj getImageForSend:indexPath.row];
[staticSceneController release];
}
in myViewClass
-(void)getImageForSend:(int)index{
imageIndexForSend = index;
}
then i am return to myViewClass after dismissing popover (popOver class) and doing some actions in myViewClass.
then i am clicking a send button.but the integer value imageIndexForSend is zero.cant get the old value which i set from PopUp.
can any one tell me a way to get the old imageIndexForSend value.may i know what mistake i done.
First, you name a method with get to set the value, it's bad.
Second, you use a property and synthesize it, so you don't need to rewrite the set method unless you need to have a custom set method.
And finally you create a new view on each selection of tableview cell !
1) Remove your getImageForSend: method, you don't need that with property
2) Instead using : [obj getImageForSend:indexPath.row];, use : obj.imageIndexForSend = indexPath.row;
3) Don't create a new view on each selection, assign the value on the existing view.
A better way to transmit data from your popover to your view (controller ?) is to have a delegate property in your popover class and set it with your view object, create a delegate protocol with a method that is called when a cell is selected in popover with an int argument (the index) then make your view class adopts the protocol and do a obj.imageIndexForSend = argument; in your protocol method.
It seems you're allocating a myView instance and assigning that to a local variable (obj), but then you don't keep a pointer to that new instance anywhere.
From what I understand, you already have an existing instance of myView, so what you need to do is to set the variable on that instance, and not create a new one every time.
Each instance have their own set of variables, so changing it in a new instance won't affect any other instances.
You are instantiating a new MyView whenever the user taps on any row of your UITableView. You should try to access the original MyView instead (or whatever object shall retain that setup value).
Within your popover, you should find a way to access the instance that holds the actual index-value. How exactly that is achieved depends a lot on your implementation.
In other words, do not instantiate something within an object that has a shorter lifetime than the object that will access that very instance.
If you're trying to access the index of the selected row in the UITableView you can just use the following:
int index = [myTableView indexPathForSelectedRow].row;
User clicks on "Remote" button and then the following UITableViewController loads up:
The user then selects any value upon which I call:
[self.navigationController popViewControllerAnimated:YES];
to go back again to the previous UITableViewController (screen shot 1).
How do I add the selected value to the UITableViewController?
I hope I am making sense.
In your remote recipients table view controller, you have the Array with which you are loading the table (say myTestArray). Keep it as a property. And also have a NSInteger property (say selectedRow) which will identify which row user selected. And when you go back to the add recipient table view controller, you can know which row was selected by
[remoteRecipientsController.myTestArray objectAtIndex:remoteRecipientsController.selectedRow];
Or use delegates. Upon row selection the remote recipients will give a call back telling which row was selected.
Update:
If you dont have access to the view controller, use delegates.
You can get both array and the selected row in the delegate method, something as follows:
-(void) remoteRecipient:(RemoteRecipientController *) remoteRecipientController didSelectRow:(NSInteger) row {
// Get the selected row
... = [remoteRecipientController.myTestArray objectAtIndex:row ];
}
Or you can also configure the delegate to just return the selected row (as your string), something as follows:
-(void) remoteRecipient:(RemoteRecipientController *) remoteRecipientController didSelectRow:(NSString *) selectedRecipient {
}
Ofcourse, In this case you need to make remote recipient controller pass the selected row as NSString in the delegate.