UISegmentedControl 在聊天情况下切换身份
我正在尝试创建一个具有内置聊天功能的应用程序。我试图使其与消息应用程序类似,但有一个主要区别。我不想添加 MMS 的相机图标,而是添加 UISegmentedControl 来手动切换对话中的人物。我让它工作得很好,除了每当你在交换机上更改身份时,它都会更改之前在聊天中所说的所有内容的身份。我真的很坚持这一点,任何帮助将不胜感激。
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;
}
<
img src="https://i.sstatic.net/Oj2fB.png" alt="图片 1"
所以总的来说,我要问的是如何修改它,以便当我切换时UISegmentedControl 仅更改开关处于此状态时键入的消息。 先感谢您!
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 UISegmentedControl 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;
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是伪代码。
1.) 将分段控件添加到此视图的 IB 中
2.) 创建以下 IBAction:
3.) 单击 IB 中的分段控件并将“valueChanged”定位到上述函数,现在只要单击分段按钮上的值,该函数就会触发
4.)对于你的聊天功能,我不知道它是如何工作的......我认为它是这样的
Here it is in psuedo-code.
1.) Add the segmented control to your IB for this view
2.) Create the following IBAction:
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
我认为这是错误的。首先,您需要更改 IBAction 函数,因为它不执行任何操作。这就是它的意思:
我希望你能同意我的观点,这没有任何作用;它所做的就是检查索引是否是您想要的,如果是,则再次检查。也许您希望更改它(只有一个等号,但这也没有意义)??,但无论哪种方式都无法解决您的问题。
其次,在类中创建一个布尔值(将其称为 leftSideConversation),如果 selectedSegmentedIndex 最初设置为 0,则将其初始化为 TRUE,如果设置为 1,则将其初始化为 FALSE。然后修改代码的 IBAction 部分,如下所示:
第三,你需要做的是创建身份......你需要知道谁在输入什么。我无法帮助你,因为我不知道它是如何编程的,但你需要找到一种方法。您的代码中的以下部分也是错误的。分段索引在整个对话过程中保持不变,并且仅在您按下按钮时才会更改。因此,谈话总是偏向一边,从你提供的截图来看,似乎一直都是这样。将代码的这一部分从: 更改
为类似于以下内容:
其中 myText 是一个布尔值,伴随着键入的每条消息,这样,如果发送消息,则 myText 设置为 TRUE,如果收到消息,则将 myText 设置为 FALSE 。祝你好运!
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:
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:
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:
to look something along the lines of this:
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!