2013-12-11 2 views
0

메시지를 수신하는 ChatViewController.mtableView에 표시됩니다. 이제 메시지를 보낸 사람이 변경되면 사용자에게 새 메시지 (alert view)로 전환 할 수있는 옵션을 제공하려고합니다. 사용자가 스위치를 클릭하면 메시지를 표시하는 방법은 무엇입니까?새 데이터로 UIViewController를 새로 고치는 방법은 무엇입니까?

메시지 수신 및 표시 방법입니다.

-(void)recvdMsg:(NSDictionary *)msg 
{ 
    NSString *currentUser = [msg objectForKey:@"sender"]; 
    NSString *match = @"@"; 
    NSString *preAt ; 
    NSScanner *scanner = [NSScanner scannerWithString:currentUser]; 
    [scanner scanUpToString:match intoString:&preAt]; 

    if (self.name == preAt) //same sender 
    { 
    NSMutableDictionary *newMsg=[[NSMutableDictionary alloc]init]; 
    NSString *m = [msg objectForKey:@"msg"]; 
    [newMsg setObject:m forKey:@"message"]; 
    [newMsg setObject:converID forKey:@"conversationID"]; 
    [newMsg setObject:@"1" forKey:@"FromTo"]; 
    NSDate *today=[NSDate date]; 

    // Convert string to date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"MM-dd-yyyy HH:mm:ss"]; 
    NSString *dt=[dateFormat stringFromDate:today]; 
    [newMsg setObject:dt forKey:@"timeStamp"]; 
    [self AddMessage:newMsg]; // adding to database. 
    NSBubbleData *heyBubble = [NSBubbleData dataWithText:m date:[NSDate date] type:BubbleTypeSomeoneElse]; 
    [bubbleData addObject:heyBubble]; 
    [bbltblView reloadData]; 
    [bbltblView scrollBubbleViewToBottomAnimated:YES]; 
} 
else  //message from new sender 
{ 
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"New Message from " message:preAt delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Switch", nil]; 
} 

} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) // cancel button clicked 
{ 
    return; 
} 
else { 

    // switch button is clicked....How to go from here.....how would i reload data? 
} 
} 
+3

사용 [의 tableview reloadData]'. 데이터 소스를 업데이트 한 후 –

+0

새 사용자의 경우 새 사용자의 메시지 만 표시하려면 경고 스위치 코드에서 bubbleData의 모든 개체를 제거하고 모든 사용자의 데이터를 bubbleData에 넣으려면 새 사용자의 메시지 만 추가하십시오. 메시지를 표시 한 다음 key "sender"의 bubbleData에 술어를 적용하고 필터링 된 배열을 사용하여 tableview에 데이터를 표시하려면 tableview에 대해 reloadData를 호출하는 것을 잊지 마십시오. –

+0

몇 가지 샘플 코드를 제공해 주시겠습니까? – icodes

답변

0

당신은

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) //OK Button 
    { 
     //ok button code 
    } 
    else //Switch Button 
    { 
     //switch button code 
     [tableview reloadData]; 
    } 
} 
+0

지금까지 경고보기를 표시하는 방법을 알았고 사용자가 어떤 버튼을 클릭했는지 알 수있었습니다. 문제는 데이터를 표시하는 방법입니다 .. – icodes

+0

현재 채팅 버디의 데이터를로드하는 데 사용했던 동일한 코드를 여기에서 사용할 수 있습니다 . –

+0

그래서 나는 다른 방법으로 그것을 사용하기 위해 사전 특성에서 메시지를 복사해야 할 것입니다. – icodes

1

alertView 대리자를 구현할 수 있습니다 귀하의 위의 if condiiton에서 완료되었습니다. 다시는 refresh해야 의미 귀하의 array 후 다시 당신이 당신의 tableview 다음과 같은 새로운 데이터를 채우기위한 reload해야합니다 -`

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) //OK Button 
    { 
     //ok button code 
    } 
    else //Switch Button 
    { 
     // when switch button is clicked....write below for reloading the data 
    [bubbleData addObject:heyBubble];// here heyBubble will be your new data 
    [bbltblView reloadData]; 
    } 
} 
+0

위의 if 문에서 테이블 뷰에 데이터를 제공하고 있기 때문에 [tableview reloadData]가 내 경우에는 아무 것도주지 않을 것이라고 생각합니다. 그렇지 않으면 stmt가 경고보기를 표시하고 있습니다. – icodes

0

잘 같은 일 아래에 당신은뿐만 아니라 당신이 가지고있는 당신의 else 상태에 따라 필요로하는 당신이 alertview 대리인 metnod을 구현할 수

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if(buttonIndex != alertView.cancelButtonIndex) { 
     // Switch action, Write your logic to reload you data 
    } 
    } 
관련 문제