【问题标题】:UISegmentedControl to switch between identities in chat situationUISegmentedControl 在聊天情况下切换身份
【发布时间】:2011-08-20 21:17:16
【问题描述】:

我正在尝试创建一个具有内置聊天功能的应用。我试图让它与 Messages 应用程序类似地工作,但有一个主要区别。我想添加一个 UISegmentedControl 来手动切换您在对话中的哪个人,而不是 MMS 的相机图标。我让它工作得很好,除非你在交换机上更改身份,它会更改之前在聊天中所说的所有内容的身份。我真的坚持这一点,任何帮助将不胜感激。

    NSString *text = [messages objectAtIndex:indexPath.row];
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];

UIImage *balloon;



if(segmentedControl.selectedSegmentIndex == 0) {

    balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
    balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height); 
}
else if(segmentedControl.selectedSegmentIndex == 1) {
    balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15);
    balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(16, 8, size.width + 5, size.height);

}



balloonView.image = balloon;
label.text = text;

return cell;

}

 -(IBAction) segmentedControlIndexChanged {
switch (self.segmentedControl.selectedSegmentIndex) 
{
    case 0: (self.segmentedControl.selectedSegmentIndex == 0);


    case 1: (self.segmentedControl.selectedSegmentIndex == 1);

        break;

    default:
        break;
}

}

总的来说,我要问的是如何修改它,以便当我切换 UISegmentedControl 时,它会更改仅在开关处于此状态时键入的消息。 提前谢谢!

【问题讨论】:

  • 您的分段控件是否与进行 UI 切换的方法挂钩?我认为您需要做的就是将一个方法连接到连接下笔尖中的 valueChanged 选项,当段更改时,该方法将被调用,您可以更新 UI。希望有帮助!

标签: iphone objective-c cocoa-touch chat uisegmentedcontrol


【解决方案1】:

这里是伪代码。

1.) 为这个视图将分段控件添加到您的 IB 2.) 创建以下 IBAction:

-(IBAction)segmentedControlValueDidChange:(id)sender
{
       if(self.segmentedControl.selectedSegmentIndex == 0)
       {
             //this is a BOOL which your app uses to know which side to create the balloons for a new text bubble
             ballonsAppearOnLeft = YES;
       }
       else
             ballonsAppearOnLeft = NO;


}

3.) 单击 IB 中的分段控件并将“valueChanged”定位到上述函数,现在只要单击分段按钮上的值,该函数就会触发 4.) 对于你的聊天功能,我不知道它是如何工作的......我假设它是这样的

-(void)addNewChat:(NSString*)chatText
{
      if(ballonsAppearOnLeft)
      {
           [self createBallonOnLeftWithText:chatText];
      }
      else
           [self createBallonOnRightWithText:chatText];


}

【讨论】:

    【解决方案2】:

    这是我认为的错误。首先,您需要更改 IBAction 函数,因为它不执行任何操作。这就是它的翻译:

    -(IBAction) segmentedControlIndexChanged {
      if(self.segmentedControl.selectedSegmentIndex == 0) 
      {
          self.segmentedControl.selectedSegmentIndex == 0;
      } 
          else if (self.segmentedControl.selectedSegmentIndex == 1)
      {
          self.segmentedControl.selectedSegmentIndex == 1;
      }
    }
    

    我希望你能同意我的看法,即这无济于事;它所做的是检查索引是否是您想要的,然后如果是,则再次检查它。也许您希望更改它(只有一个等号,但这也没有意义)??,但无论哪种方式都无法解决您的问题。

    其次,在您的类中创建一个布尔值(称为 leftSideConversation),如果 selectedSegmentedIndex 最初设置为 0,则将其初始化为 TRUE,如果设置为 1,则将其初始化为 FALSE。然后将代码的 IBAction 部分修改为如下所示:

    -(IBAction) segmentedControlIndexChanged {
      if(self.segmentedControl.selectedSegmentIndex == 0) 
      {
          leftSideConversation = TRUE;
      } 
          else if (self.segmentedControl.selectedSegmentIndex == 1)
      {
          leftSideConversation = FALSE;
      }
    }
    

    第三,您需要做的是创建身份......您需要知道谁在输入什么内容。我无法帮助你,因为我不知道它是如何编程的,但你需要想办法。您的代码中的以下部分也是错误的。段索引在整个对话过程中保持不变,只有在您按下按钮时才会更改。因此,谈话总是在一边,从你提供的快照来看,似乎一直都是这样。更改这部分代码:

    if(segmentedControl.selectedSegmentIndex == 0) {
    
    balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
    balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height); 
    }
    else if(segmentedControl.selectedSegmentIndex == 1) {
        balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15);
       balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
        label.frame = CGRectMake(16, 8, size.width + 5, size.height);
    }
    

    从这个角度来看:

    if(myText == leftSideConversation) {
    
    balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
    balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
    label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height); 
    }
    else {
        balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15);
       balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
        label.frame = CGRectMake(16, 8, size.width + 5, size.height);
    }
    

    其中 myText 是一个布尔值,伴随每条输入的消息,如果您发送消息,myText 设置为 TRUE,如果您收到消息,则设置为 FALSE。祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 2011-08-10
      • 1970-01-01
      • 2021-01-15
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多