两个UITextView的UIActionSheet

时间:2013-10-21 07:21:24

标签: ios uitextview uiactionsheet

我有一个UIActionSheet来更改UITextView的字体。我的问题是我已经添加了另一个UITextView,现在想要它取决于打开的UITextView,(我的意思是打开编辑,显示键盘)给我一个源的类型在一个或另一个...不是哪个是UITextView的正确属性,以便根据选择区分彼此。

任何想法?

这是单个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;        
}

我看到使用textview,可以使用多种方法,特别是可以使用它,将在你开始编辑textview时运行。

(void)textViewDidBeginEditing:(UITextView *)textView

但我不知道如何在我的代码中实现它......

2 个答案:

答案 0 :(得分:5)

您可以通过设置setTag属性为每个UITextView应用标记,并且可以区分两者。 添加属性以检查哪个textView处于可编辑状态

@property NSInteger editabelTextView;

现在分配所有UITextView并为每个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;

现在将textView.tag分配给self.editabelTextView

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

现在检查self.editabelTextView并为特定文本视图记录setFont

- (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;
  }
}

答案 1 :(得分:0)

添加

@property UITextView *activeTextView;

现在在textView Delegate

-(void)textViewDidBeginEditing:(UITextView *)textView

{
_activeTextView=textView;
}

然后,将UIActionsheet委托编辑为

- (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;        
}