I’ve got the following code which is a segmentedControl for a language selection option. Case 0 is for English and case 1 is for Greek which is then passed to a UITextView. The problem is that every time I press the button for case 1 or 0 I have to go back to the previous view controller which is a table view controller and come back to the “detailViewController” for the correct language to be displayed. Is there a way for this to be done without switching back and forth between the view controllers? Something like a small animation or a refresh or a reload method for UITextView.
int a;
- (IBAction)languageSeg:(id)sender {
switch (((UISegmentedControl *)sender).selectedSegmentIndex) {
case 0:
a=0;
break;
case 1:
a=1;
break;
default:
break;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
UIImage *saladImage = [UIImage imageNamed:@"salad.jpg"];
UIImage *fishImage = [UIImage imageNamed:@"fish.jpg"];
[textDetail setScrollEnabled:YES];
[textDetail setContentSize:CGSizeMake(320, 190)];
//Switch the UIImages and UILabel based on item
switch (itemNumber) {
case 0:
itemName.text = @"Salad";
itemPic.image = saladImage;
self.title = @"Salads";
if (a==0){
textDetail.text = @"description in english";
}
if (a==1){
textDetail.text = @"description in greek";
}
break;
case 1:
// etc.
You can update the text for the textView in the languageSeg: method the same way you set it initially in viewWillAppear:
I would create a separate method that handles the language switch, then call that method from both viewWillAppear and languageSeg and anywhere else you need it.
For example, your language setter method would include the code currently at the bottom of viewWillAppear:
Then your viewWillAppear: method would look like this:
And your languageSeg: method would look like this:
A lot of implementation would be dependent on how the rest of your code is set up and your individual needs, but this is the general idea.