iOS Development Chat Module--Content Preservation Logic implementation

Source: Internet
Author: User

Detailed Requirements:

In the actual development, it is possible in the late optimization, there will be the need to optimize the needs: Chat input box to save the text entered before, improve the user's good experience.

In the chat module, the user may enter a number of characters in the input box, but do not click to send to exit chat, or to click on the user's avatar to confirm the user's information, or for example, need to send a friend another friend's id have to temporarily quit the current friend chat interface to find another interface for the ID, However, the current chat input box has already entered several characters, and the user certainly does not want to delete the previously typed text after exiting. So here we need to temporarily save the string that the user has typed but not sent out.

However, the need to meet 1, completely kill or completely exit the application needs to clear this temporarily saved string, 2, sent out, must be delegate before the temporary save string.

Begin:

This part of the logic of the implementation of the first I did not much better clue, only think of local serialization, but actually this is not the best idea, because the local serialization used here a bit of a fuss, in fact, as long as the global static variable dictionary can be.

Concrete implementation of the logic, I also deliberately read the study of the implementation of the coding project, after all, the project is a relatively mature project, chat module is also doing a very good, so learn other people's thoughts, is the so-called standing on the shoulders of giants, is also very good oh.

So below, I will directly read coding source (Learning Coding-ios Open Source project log (a)) in this chat module content preservation logic, do not take their own work development projects.

1, first declare the global static variable, coding with inputstrdict to store the input box string, and inputmediadict I do not know what it is specific, it should be media and other elements:

2, then a lot of logic encapsulated in this Uimessageinputview class, methods are not public, the full use of the logic of Uimessageinputview activity cycle can be.

1#pragma mark Remember input23-(Nsmutabledictionary *) shareinputstrdict{4if (!_inputstrdict) {5 _inputstrdict =[[Nsmutabledictionary alloc] init];6}7Return_inputstrdict;8}9Ten-(Nsmutabledictionary *) shareinputmediadict{11if (!_inputmediadict) {_inputmediadict =[[Nsmutabledictionary alloc] init];13}14Return_inputmediadict;15}16-(NSString *) inputkey{NSString *inputkey =Nil19if (_contenttype = =UIMESSAGEINPUTVIEWCONTENTTYPEPRIMSG) {Inputkey = [NSString stringWithFormat:@"privatemessage_%@", Self.toUser.global_key];21}Else{22If(_commentofid) {23Switch(_contenttype) {24CaseUimessageinputviewcontenttypetweet:Inputkey = [NSString stringWithFormat:@"tweet_%@_%@", _commentofid.stringvalue, _touser.global_key.length >0? _touser.global_key:@""];26Break;27CaseUimessageinputviewcontenttypetopic:Inputkey = [NSString stringWithFormat:@"topic_%@_%@", _commentofid.stringvalue, _touser.global_key.length >0? _touser.global_key:@""];29Break;30CaseUimessageinputviewcontenttypetask:Inputkey = [NSString stringWithFormat:@"task_%@_%@", _commentofid.stringvalue, _touser.global_key.length >0? _touser.global_key:@""];32Break;33Default:34Break;35}36}37}38ReturnInputkey;39}40-(NSString *) inputstr{NSString *inputkey =[Self inputkey];43If(Inputkey) {Debuglog (@"inputstr_get:%@", [[Self shareinputstrdict] objectforkey:inputkey]);45Return[[Self shareinputstrdict] objectforkey:inputkey];46}47ReturnNil48}4950-(void) deleteinputdata{Wuyi NSString *inputkey =[Self inputkey];Debuglog (@"inputkey_delegate:%@", Inputkey);53If(Inputkey) {54[[Self shareinputstrdict] removeobjectforkey:inputkey];55[[Self shareinputmediadict] removeobjectforkey:inputkey];56}57}5859-(void) saveinputstr{NSString *inputstr =_inputtextview.text;NSString *inputkey =[Self inputkey];Debuglog (@"inputkey_save:%@", Inputkey);63if (Inputkey && inputkey.length >0) {64if (inputstr && inputstr.length >0) {65[Self shareinputstrdict] setobject:inputstr Forkey:inputkey];66}Else{67[[Self shareinputstrdict] removeobjectforkey:inputkey];68}69}70}7172-(void) saveinputmedia{NSString *inputkey =[Self inputkey];74if (Inputkey && inputkey.length >0) {(_medialist.count > 0) {[[Self shareinputmediadict] setobject:_medialist F Orkey:inputkey]; }else{[[Self shareinputmediadict] removeobjectforkey:inputkey]; the} Bayi}-(Nsmutablearray *) Inputmedia {NSString *inputkey = [self Inputkey], if (inputkey) {* * * return [[self shareinputmediadict] objectforkey:inputkey] ; (a) the return of nil; (void) Settouser: (User *) touser{_touser = touser; NSString *inputstr = [self inputstr]; 94 if (_inputte Xtview) {_contenttype! = uimessageinputviewcontenttypeprimsg) {Self.placeholder = _touser? [NSString stringwithformat:@ "Reply%@", _touser.name]: @ "Write a review"; }else{98 Self.placeholder = @ "Please enter private messages";}100 _inputtextview.selectedrange = Nsmakerange (0, _inputtextview.text.len GTH); 101 [_inputtextview inserttext:inputstr? INPUTSTR: @ ""];102 103 _medialist = [self inputmedia];104 [self mediaListCh enged];105}106} 

Above is the name of the chat object is stitched into a key value, and then corresponding to the storage of the current input box string into the global static dictionary, and then the removal, deletion of several methods.

3. Then see where these methods are called:

Save the method, placed in the method of the frame rewrite, because the input box will be the keyboard's reality and hide to switch frame, but my company's project start chat module is my colleague developed, I found that he used masonry layout code to change the position of the input box, Choosing a layout constraint also means discarding the frame, so where to call the Save method is implemented based on actual requirements and actual coding. In addition, in the beginning of the first development of this input box, you can consider the period of its operation: start editing---edit-end editing, these operating cycles can be implemented in their own way, and the life cycle of a controller. In short, a lot of ideas, do well is to achieve a good management of good maintenance logic.

Then find the method of deletion, delete the method is placed in the string out of the first, because it has been sent out, can be stored in the dictionary to delete the elements.

In addition, when the key is created, this key string is dependent on the current chat object, because the current input box content and the current friend object one by one corresponding, can not I saved the current friend corresponding input box content, jumped to another friend but appeared the same content. So the key value needs to be determined according to the current friend's string, so the coding source overrides the Touser property's Set method:

Respecting the fruits of labor: http://www.cnblogs.com/goodboy-heyang/p/5782201.html

iOS Development Chat Module--Content Preservation Logic implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.