2011-08-20 91 views
1

我正在嘗試創建一個具有內置聊天功能的應用程序。我試圖使其與Messages應用程序的工作類似,但有一個主要區別。而不是MMS的攝像頭圖標我想添加一個UISegmentedControl來手動切換你正在談話的人。除非您在交換機上更改身份,否則我的工作狀況非常好,它會更改聊天中以前所說的所有內容的身份。我真的堅持這一點,任何幫助將不勝感激。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; 
} 

}

Image 1 Image 2

所以在所有什麼我問的是我怎麼能修改此,使它所以當我切換UISegmentedControl會在交換機僅處於此狀態時更改輸入的消息。 提前謝謝!

+0

您的分段控件是否支持使用UI切換的方法?我認爲你所需要做的就是將一個方法連接到nib下的valueChanged選項下,並且當這個段改變時,這個方法會被調用,並且你可以更新UI。希望有所幫助! – msgambel

回答

1

這是我認爲是錯誤的。首先,你需要改變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,則將其初始化爲FUESE。然後修改代碼中的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。祝你好運!

2

這裏是僞代碼。

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]; 


}