-2

I have a UIActionSheet to change font of a UITextView. The problem I have is that I have added another UITextView and now wants that it depends on the open UITextView, (I mean open to edit, showing the keyboard) assign me the type of source in one or the other... not be which is the correct property of the UITextView in order to differentiate one from the other depending on the selection.

any idea?

This is the case of a single UITextView.

- (IBAction) displayFontPicker:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select a font" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Helvetica", @"Courier", @"Arial", @"Zapfino", @"Verdana", nil];
[actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES];

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
selectedButtonTitle = [selectedButtonTitle lowercaseString];

if ([actionSheet.title isEqualToString:@"Select a font"])
switch (buttonIndex){

    case 0:

        [_textView setFont:[UIFont fontWithName:@"Helvetica" size:25]];

        break;
    case 1:

        [_textView setFont:[UIFont fontWithName:@"Courier" size:25]];

        break;        
}

I'm seeing that using the textview , can use a number of methods, in particular this could use it, will run when you start to edit a textview.

(void)textViewDidBeginEditing:(UITextView *)textView

But I don't know how to implement it in my code...

user1908661
  • 89
  • 2
  • 9

2 Answers2

0

You can apply tag for each UITextView by setting setTag property and can differentiate between both. add property to check which textView is in editable state

@property NSInteger editabelTextView;

Now allocation all UITextView and add tags for each UITextView.

self.textView1 = [[UITextView alloc] init];
self.textView2 = [[UITextView alloc] init];
self.textView3 = [[UITextView alloc] init];

self.textView1.tag = 1;
self.textView2.tag = 2;
self.textView3.tag = 3;

and now assign textView.tag to self.editabelTextView

-(void)textViewDidBeginEditing:(UITextView *)textView
{
  self.editabelTextView=textView.tag;
}

now check self.editabelTextView and accrodingly setFont for specific text view

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex {
  switch (self.editabelTextView){
  case 1:
      [self.textView1 setFont:[UIFont font...]];
      break;
  case 2:
      [self.textView2 setFont:[UIFont font...]];
      break;
  case 3:
      [self.textView3 setFont:[UIFont font...]]
      break;  
  default:
      break;
  }
}
nikhilgohil11
  • 958
  • 14
  • 24
0

add

@property UITextView *activeTextView;

now in textView Delegate

-(void)textViewDidBeginEditing:(UITextView *)textView

{
_activeTextView=textView;
}

then, edit the UIActionsheet delegate as

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
selectedButtonTitle = [selectedButtonTitle lowercaseString];

if ([actionSheet.title isEqualToString:@"Select a font"])
switch (buttonIndex){

    case 0:

        [_activeTextView setFont:[UIFont fontWithName:@"Helvetica" size:25]];

        break;
    case 1:

        [_activeTextView setFont:[UIFont fontWithName:@"Courier" size:25]];

        break;        
}
Raon
  • 1,186
  • 3
  • 10
  • 25
  • How does this resolve the issue? – Popeye Oct 21 '13 at 07:32
  • He needs which textfield was opened for editing right??here activeTextfield will have the last edited textView... – Raon Oct 21 '13 at 07:34
  • It's not required, but it is really annoying and for me I don't accept answers that don't have an explanation included. I know exactly what your code is doing but for those that don't an explanation would be great, so I was hoping you would edit your answer not comment. It can only improve your answer. – Popeye Oct 21 '13 at 07:48
  • Thanks to the two. I have tried the option of Raon, but don't call the method "-(void)textViewDidBeginEditing:(UITextView *)textView ", I added a NSLog and does not show anything when you start to edit the uitableview. I've been looking on how to solve the problem and declared the activeTextView as a delegate. activetextview.delegate=self; within the own method and within the viewdidload but doesn't work. – user1908661 Oct 21 '13 at 14:23