Problem Abstraction:
Now in a music playback module, there are three status switches for the music playback order, as shown in
Sequential playback
Single cycle
Random Play
Now I'm just going to abstract the simplest part, described in the interactive process of the button operation,
(1) button default state is "sequential play" song mode
(2) Keep clicking button, the song cycle mode toggle sequence is "sequential play, single loop---shuffle play," Sequential play->
(3) Each click Set a song cycle mode, all the loop mode sent to the server, the next time you come to the interface, read data from the server, automatically loaded in the correct mode, not necessarily "sequential play" cycle mode
Believe this simple scene, everyone is very clear.
Solution Solutions
-----------------Split Line----------------my own solution----------------the following---------------------------
I think
(1) The button has a total of three states, so an enum enumeration is defined, representing 3 of the song playback cycle mode,
enum{soundplayqueue,//sequential play soundplaysingle,//single loop soundplayrandom,//random play};
(2) using the button's Tag property as the status of the song playback, statusfromserver as the song playback state of the settings read from the server (it has three values, 00-sequential play, 01-single loop, 02-random play), then the first time to join the interface, The tag of the button must be assigned a value
- (void) viewdidload{ [super viewdidload]; int Statusfromserver = [song cycle mode from the server-there are three values (00,01,02)]; if (statusfromserver = = 00) { playBtn.tag = Soundplayqueue; [playbtn setimage: Play the picture sequentially]; } else if (statusfromserver == 01) { playBtn.tag = SoundPlaySingle; [playbtn setimage: Single cycle picture]; } Else if (statusfromserver == 02) { playbtn.tag = soundplayrandom; [playbtn setimage: Random Play Picture]; &Nbsp; }}
(3) Click button, add button click Method-playbtnclicked, switch songs cycle mode,
- (void) playbtnclicked: (ID) sender{ uibutton tempbtn = (UIButton *) sender; if (tempbtn.tag == soundplayqueue) { //If you are playing sequentially, the next mode is set to single loop, tempbtn.tag = soundplaysingle; [tempbtn setimage: Single cycle picture]; statusfromserver = 01; } else if (Tempbtn.tag == soundplaysingle) { //if the single loop is now, then the next mode is set to shuffle, tempBtn.tag = SoundPlayRandom; [tempbtn setimage: Random Play Picture]; statusfromserver = 02; } else if (Tempbtn.tag == soundplayrandom) { //If you play now randomly, the next mode is set to play sequentially, tempbtn.tag = soundplayqueue; [ Tempbtn setimage: Play the picture sequentially]; statusfromserver = 00; }}
Self-Summary:
I did solve the problem in this way, but I always felt that the code was too cumbersome and unreasonable,
(1) One of the main problems is that button acts as a UI control, it is only used for interface display and event control, but my side uses its properties to identify the current song loop mode, which means that the button has done more than its business category;
(2) Next is this way, undoubtedly increased the current Viewcontroller in the code, yes Viewcontroller looks cluttered
Help:
Dear you, dear God, can you give me directions to help me optimize the code? Hope to point out a way of thinking on it, do not need detailed code, again this code farm kneeling thanks. ^^
iOS Development Ideas Question (series 1: How to switch the three states of a button?) )