开发者

UISegmentedControl to switch between identities in chat situation

I am trying to create an app with a built in chat feature. I am trying to make it work similarly to the Messages application, with one major difference. Instead of there being a camera icon for MMS I would like to add a UISegmen开发者_开发知识库tedControl to manually switch between which person you are in the conversation. I have it working pretty well except whenever you change identity on the switch, it changes the identity of everything said previously in the chat. I'm really stuck up on this and any help would be greatly appreciated.

    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 to switch between identities in chat situation

UISegmentedControl to switch between identities in chat situation

So over all, what I'm asking is how can I modify this, to make it so when i switch the UISegmentedControl it changes the messages typed while the switch is in this state only. Thank you in advance!


Here it is in psuedo-code.

1.) Add the segmented control to your IB for this view 2.) Create the following 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.) Click your segmented control in IB and target "valueChanged" to the above functions, now the function will fire whenever the value on the segmented button is clicked 4.) For your chat function, I have no idea how it works...I assume it goes like this

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


}


This is what I think is wrong. First, you need to change the IBAction function, because it doesn't do anything. This is what it translates to:

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

I hope you can agree with me that this does nothing; what it's doing is checking if the index is what you want it to be, then if it is it just checks it again. Maybe you wanted it to be changed (one equal sign only, but that won't make sense either)??, but either way that won't solve your problem.

Second, create a boolean in your class (call it leftSideConversation), initialize it to TRUE if the selectedSegmentedIndex is initially set to 0, and FALSE if it's set to 1. Then modify the IBAction part of your code to look like this:

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

Third, what you need to do is create identities ... you need to know who is typing what. I can't help you with this, because I do not know how it was programmed, but you need to figure out a way. The following part in your code is what's wrong as well. The segment index remains the same throughout the conversation, and is only changed when you press the button. Hence, the conversation is always on one side, and judging from the snapshots you provided, it seems this has always been the case. Change this part of your code from:

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

to look something along the lines of this:

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

where myText is a boolean that accompanies each of the messages that are typed, such that myText is set to TRUE if you send the message and FALSE if you receive it. Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜