Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 33    Views: 90

Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
11/07/12 01:37 AM (13 years ago)

Objective C code to JSON help.

Hi David. I'm into a new plugin. I have a UIImageView and a UITextView. Now I load in the images and text directly in ViewDidLoad method I uses the code: self.image = [NSArray arrayWithObjects: [UIImage image named: @ "image1.jpg"]; [UIImage image named: @ "image2.jpg"], [UIImage image named: @ "image3.jpg"], [UIImage image named: @ "image4.jpg"], [UIImage image named: @ "image1.jpg"]; [UIImage image named: @ "image2.jpg"], [UIImage image named: @ "image3.jpg"], [UIImage image named: @ "image4.jpg"], [UIImage image named: @ "image1.jpg"]; [UIImage image named: @ "image2.jpg"], [UIImage image named: @ "image3.jpg"], [UIImage image named: @ "image4.jpg"], nil]; And self.textview = [NSArray arrayWithObjects: @ "Bla bla bla1", @ "Bla bla bla2", @ "Bla bla bla3", @ "Bla bla bla4", @ "Bla bla bla5"; @ "Bla bla bla6", @ "Bla bla bla7", @ "Bla bla bla8", @ "Bla bla bla9", @ "Bla bla bla10", @ "Bla bla bla11", @ "Bla bla bla12", nil]; But how do I do if it will use the JSON? I think it should look like this: Self.image = [[NSMutableArry alloc] init]; NSArray * imageforplugin = [[self.screendata jsonVar] objectForKey: @ "image items"]; [imageforplugin count]; or something like that?
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
11/07/12 08:38 AM (13 years ago)
smells like an animation sequence..... use this for IOS: [UIImage imageNamed:[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"image1":@""]], and then you reference the "image1" by making entries for the value in the index.php file, right after the NICKNAME property. (@ line 300)
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/07/12 09:44 AM (13 years ago)
Hi Atrain. No it is not a animation. It load each image and text on every page in my page plugin. https://dl.dropbox.com/u/83876419/Bt/Pagebook%20plugin%20demo.mov I want to connect this to CP. The you have options to "page" wiht only full screen image or half image and half whit scrolling text. With this plugin you can make story book or image catalog or only scrolling text book etc. So I need some connection help to make it to be a plugin.
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
11/07/12 10:36 AM (13 years ago)
check this thread out then- https://www.buzztouch.com/forum/thread.php?tid=61A19811BADC9373C0AC264&command=isSearching&currentPage=1&topicTitle=json&createdBy=&repliedBy=&minViews=-1&maxViews=-1&minReplies=-1&maxReplies=-1&forumCategory= that unraveled the mystery for me to connecting my code to the CP. the build a plug-in webinar is recommended too...
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/07/12 11:34 PM (13 years ago)
Naturally there are a few different ways of doing this. In most cases, you'll want to "loop" through the JSON data and add objects to your array one by one until it's full. This happens in the list menu, button menu, maps, quiz, etc. I'm thinking something like this: a) A parent screen. This screen has properties for miscellaneous settings for the "story" b) Child item. Each child items represents a "page" in your "story" c) Your code has a NSMutableArray that begins empty. d) Your code "loops" through the child items for each page and populates the array (just like the menus and all the other screens that use child items) e) Each child item has text, image, etc f) You do your magic with the NSMutableArray of objects.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/12 12:31 AM (13 years ago)
@David Can you show a Sample how the Json code will look like? So I have somthing to start with. I have gave me the hell that I will fix this. :)
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/08/12 01:59 AM (13 years ago)
It's probably easiest to look at a menu with few items in it. The JSON will have that structure. I say easier because it's in your control panel. {"itemId":"1111", "itemType":"yourPlugin", "itemNickname":"Story Screen", "navBarTitleText":"My Story", "childItems":[ {"itemId":"1", "itemType":"MC_pageItem", "text":"the text 1", "imageName":"image.png"}, {"itemId":"2", "itemType":"MC_pageItem", "text":"the text 2", "imageName":"image.png"}, {"itemId":"3", "itemType":"MC_pageItem", "text":"the text 3", "imageName":"image.png"}, {"itemId":"4", "itemType":"MC_pageItem", "text":"the text 4", "imageName":"image.png"} ] } The idea is that you would create whatever properties for each page you need. I'm assuming text and image name here. You may have other options like transition type, background / forground, etc. Look through the child items, fill an NSMutableArray full of BT_items (just like each menu item, quiz question, map location, etc). Have a look at those files to see where the arrays are loaded up (I think the method is called parseData in most of these files). After you array is loaded up, you can do whatever you need to show / hide individual screens pages.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/12 02:06 AM (13 years ago)
Thanks David!
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/12 02:22 AM (13 years ago)
One more question. in my .m file How do I do to get the text in the textView and the image in my imageView? This i so tricky to understand. When I look at a menu it only fill the tableView. I most bee stupid or dumb.
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/08/12 02:33 AM (13 years ago)
No stupid. Not dumb. Dunno, lets see if I can figure out what you're up to....hmm.... BT_item holds some JSON data. OK. You need to get the value for each item in the JSON for each text / image. //get the 3rd story (it starts at zero) from your NSMutableArray BT_item *theStoryItem = [yourArray objectAtIndex:2]; //get the value of the text... NSString *textForStory = [BT_strings getJsonPropertyValue:theStoryItem.jsonVars:@"textForStory":@""]; //set the value of some UITextView or UILabel or whatever.... [myLabel setText:textForStory]; //get image name... NSString *imageName = [BT_strings getJsonPropertyValue:theStoryItem.jsonVars:@"imageName":@""]; //set an image view... [myImageView setImage:[UIImage imageNamed:imageName]];
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/12 02:50 AM (13 years ago)
Ohh !! No we talking. Ok I will try this. Thanks Again!
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/12 07:12 AM (13 years ago)
Yes. I get it to work, but it not the nice plugin backend in the world! :) When I send it for review you and parker gone have a big laugh. But now can I control it from my control panel.
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
11/08/12 08:26 AM (13 years ago)
Nice. I was looking at the 1.5 advanced menu code a while back and recall you worked on that- I can't even imagine how 1.5 custom plugins were built and conected to the Control Panel. I ried and gave up quicky once i stared to understand how 2.0 worked. The BT fairies will clean that up and make it sparkle. Excited to see this and see you connecting to the CP now @mack! (and more great tips David!)
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/09/12 06:49 AM (13 years ago)
Yes. I get it to work, but it not the nice plugin backend in the world! :) When I send it for review you and parker gone have a big laugh. But now can I control it from my control panel.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/12/12 05:23 AM (13 years ago)
@ David. I have add this to my bt_config.txt {"itemId":"my id from my cp", "itemType":"myPlugin name", "itemNickname":"Story Screen", "navBarTitleText":"My Story", -----This Works----- "childItems":[ {"itemId":"1", "itemType":"MC_pageItem", "text":"the text 1"}, {"itemId":"2", "itemType":"MC_pageItem", "text":"the text 2"}, {"itemId":"3", "itemType":"MC_pageItem", "text":"the text 3"}, {"itemId":"4", "itemType":"MC_pageItem", "text":"the text 4"} ] ---------------The childItems do not work----------------- } BT_item *What do I write here????? = [My array objectAtIndex:2]; //get the value of the text... NSString *textToTextView = [BT_strings getJsonPropertyValue:Here it will be same as BT_item.jsonVars:@"text":@""]; //set the value of some UITextView or UILabel or whatever.... [MyTextView setText:textToTextView]; How can I take text from childItems and put them in my textView?
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/12/12 12:40 PM (13 years ago)
"what do I write here" The idea is that you choose a meaningful name for the BT_item you're referencing. The name doesn't matter. Like: BT_item *theStoryItem = [yourArray objectAtIndex:2]; "theStoryItem" is just a name I chose and it refers to the BT_item object in the second position of the "your array" data object.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/12/12 01:40 PM (13 years ago)
hmmm ? I most missing somthing here! I want the app to read the Json childItems. I want to be able to change the text from CP. If I use [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"text":@"change text in the CP"]]; it works but it it only one string/value. If we forget my page book app fore a while. If I create a button and then a lable in my XIB file. Here is my code in my .m file. -IBAction { //every time I hit the button I want the label to change text from the JSON. What is the code here to show more and one string? [self.myLabel setText]; { And how do Json look like ? When I use this BT_item *textOne = [My array objectAtIndex:2]; //get the value of the text... NSString *textToTextView = [BT_strings getJsonPropertyValue:textOne.jsonVars:@"text":@"this text shows up in my lable"]; //set the value of some UITextView or UILabel or whatever.... [MyTextView setText:textToTextView]; The default Value shows but not the Json Value "text":"Now you've got your text from json", I really want to learn to understand this.
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/13/12 02:02 AM (13 years ago)
Lets back up a bit and see if we can help this along... There are two main concepts. One is the idea behind a BT_item. What the heck is a BT_item? A BT_item is an object that can represent just about anything. It can be a screen, a menu item, an image, a quiz question, or in your case, something totally different. That's all OK. The idea is that a BT_item can hold any type of JSON. So, on a screen without any "child item" - like a Custom URL screen. There is only one BT_item and it represents the screen's JSON data. On screens with "child items" like quizzes, maps, menus, and other things. there is one BT_item that represents the screen's data JSON data and a LIST of BT_items that represent the child items on the screen (quiz questions, images, buttons, etc). In your case, you have SCREEN data for the screen and childItem data for the "pages" in your story book. Because you have child items, you need to put these in a list - an array. All of the story items go in an array. You can see how this is done in all the other screens with child items. Fill up an array of BT_items. Next, you need to figure out 'which one' you want to access before you can set any text or get anything for one of the pages. I think you understand this. This line: NSString *textToTextView = [BT_strings getJsonPropertyValue:textOne.jsonVars:@"text":@"this text shows up in my lable"]; in english means: "Hey BT_strings class, look at the JSON data for textOne object (the BT_item) and find me the value of the "text" property. If it does not exist, return the default value." Again, I think you get this. But, if it's returning the default value then the textOne object must either be empty or not what you want. You'll need to figure out a way to test / debug each of these items as you fill up your array of child items. You must be doing some sort of loop to fill up the array. Look at this loop, print out (NSLog) the values of each item while you're looping through them to figure out what you're looking at.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/13/12 05:40 AM (13 years ago)
Hi David. I have look at menus and quiz. Do I need all this method to get it to work? -(void)loadData -(void)downloadData -(void)parseScreenData I have try it but I can not get json data in the childItems. this line: "text":"the text 1" But if i write it after background it work. {"itemId":"62B5857BE39A275946FFE5D", "itemType":"Quotes", "itemNickname":"Story Screen", "navBarTitleText":"My Story", "backgroundColor":"#FF0000", "childItems":[ {"itemId":"1", "itemType":"btQuotes", "text":"the text 1"}, {"itemId":"2", "itemType":"btQuotes", "text":"the text 2"}, {"itemId":"3", "itemType":"btQuotes", "text":"the text 3"}, {"itemId":"4", "itemType":"btQuotes", "text":"the text 4"} ] }
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/14/12 12:15 AM (13 years ago)
"Do I need all this method to make it work" - YES. You need a way to "read" the values for each JSON item into BT_item objects. This will allow you to retrieve the JSON for any given object at any time. These are the methods that load up the NSMutableArray with BT_items, one each for each childItem in the JSON
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/14/12 12:26 AM (13 years ago)
Ok! I will try again to day. See if can make it. Thanks David for all your help.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/14/12 08:32 AM (13 years ago)
I GIVE UP NOW!! I have try for 8 hours now. I have take all method from BT_Menu and implement it ,and then I take away the all thing with tabview. Then I have look row for row to how. Make me nuts. What I can understand BT_menu save the json data first, then load it and then builds the screen. Have I understand that right? I have try to load my Array in -(void)layoutScreen -(void)viewWillAppear -(void)viewDidLoad willAppear and DidLoad is the same. It only show me the default string if it not find json data. layoutScreen did not work at all. Where should I implement my array? I look at all that has made a plugin that there is no one who uses one NSMutableArray. It is so much harder than just using a NSString with a value. It is only Crown with his Circle View List Menu plugin that has a NSMutableArray. But he has just tweak bt_simple menu. yes yes what is a day at the castle! :))
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/14/12 05:03 PM (13 years ago)
Giving up...pout face :-( I can only assume what you're trying to do - hard to guess sometimes. But, for sure you'll run into trouble if you can't understand how to get your JSON childItems into an array. You'll need to get them into an array so you can access them later. I have an idea: Maybe send Warren Evans a message with a Webinar event title for this exact problem? I'm happy to lead a webinar to cover this and this alone.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/15/12 01:56 AM (13 years ago)
I have written a message to Warren. Hope we can reach a solution to this then.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/15/12 02:19 AM (13 years ago)
Yes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!. I managed to solve this. shame on those who giving up!! :) I'm so happy now! Damn it should be so difficult. But I still want us to create a webinar on this. It's useful for others to learn about how the algorithm works, what you need to consider etc. It is great for those who want to create more avandcerade plugins. Wishes there were more like you David that pushes us forward in development.
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
11/15/12 11:36 PM (13 years ago)
So cool! Looking forward to the webinar. I would love to have this for Android too....maybe I can help. Planning to start studying Android seriously tomorrow.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/21/12 02:27 AM (13 years ago)
@David. Super cool webinar. You done a great job. Too bad I did not get home in time from my conference to see and hear it live. I have now looked at Warren recording of the webinar. (Super thanks to you Warren if you see this.) I wonder a little thing about json ChildItems. If I have two or three buttons. That can use three different array. Can json code look like this then? "child items": [ {"itemId": "1", "itemtype": "for the button1", "text": "Load text index 0"}, {"itemId": "2", "itemtype": "for the button1", "text": "Load Text Index 1"}, {"itemId": "3", "itemtype": "for the button1", "text": "Load text index 2"}, {"itemId": "4", "itemtype": "for the button1", "text": "Load text index 3"}, {"itemId": "5", "itemtype": "for the button2", "text": "Load text index 0"}, {"itemId": "6", "itemtype": "for the button2", "text": "Load Text Index 1"}, {"itemId": "7", "itemtype": "for the button2", "text": "Load text index 2"}, {"itemId": "8", "itemtype": "for the button2", "text": "Load text index 3"} ]   or "child items": [ {"itemId": "1", "itemtype": "for the button1", "text": "Load text index 0"}, {"itemId": "2", "itemtype": "for the button1", "text": "Load Text Index 1"}, {"itemId": "3", "itemtype": "for the button1", "text": "Load text index 2"}, {"itemId": "4", "itemtype": "for the button1", "text": "Load text index 3"} ] "child items": [ {"itemId": "5", "itemtype": "for the button2", "text": "Load text index 0"}, {"itemId": "6", "itemtype": "for the button2", "text": "Load Text Index 1"}, {"itemId": "7", "itemtype": "for the button2", "text": "Load text index 2"}, {"itemId": "8", "itemtype": "for the button2", "text": "Load text index 3"} ]
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/21/12 03:00 AM (13 years ago)
If you have two buttons, you'll only have two child items. If you have three buttons, you'll have three child items. If you have four buttons, you'll have four child items. Each child item represents one button. "child items": [ {"itemId": "1", "itemtype": "button", "buttonLabel": "button label 1", "loadScreen":"122", "color":"#ffffff"}, {"itemId": "2", "itemtype": "button", "buttonLabel": "button label 2", "loadScreen":"233", "color":"#999999"}, {"itemId": "3", "itemtype": "button", "buttonLabel": "button label 3", "loadScreen":"1434", "color":"#989898"}, {"itemId": "4", "itemtype": "button", "buttonLabel": "button label 4", "loadScreen":"1232", "color":"#232323"} ]
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/21/12 07:06 AM (13 years ago)
Ok I understand. Now I have another question. I have a button that will load a random text from childItems. - (IBAction) quote_btn_touch: (id) sender { [self.loadQuotes]; } - (void)loadQuotes{ / / How do I do this for it to load the index of childItem random with no repeats? / / I do not know how I can get to a counting function here. example int rQuotes = ran ()% 10; bla blabla [self.quote_text setText: blah blah]; } I appreciate your help.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/21/12 07:23 AM (13 years ago)
This works but I don't now if it right to do. But this will only random the text in my array. I want it to random but not to repeats. Do you have a suggestion on how I should do? - (void)loadQuotes{ int rNumber = rand() % 10; BT_item *radomQoutes = [Quoteslist objectAtIndex:rNumber]; NSString *firstQoutes = [BT_strings getJsonPropertyValue:radomQoutes.jsonVars:@"text":@""]; [self.quote_text setText:firstQoutes]; }
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
11/22/12 01:11 AM (13 years ago)
1) Load the quotes in an array. 2) Randomize this array (google or look at the quiz plugin, it randomizes the questions). 3) Show the quotes one by one. I don't know when you're showing each quote. If you're showing more than one of them, like one, then the next, then the next, just show each one in the array you mixed up.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/02/12 03:35 PM (13 years ago)
Hi David. You could well have said that you should change hasChildItems: No to hasChildItems: Yes. Just kidding. I could easily have asked. :) Now you've got my qoute plugin with childitems working. Now, it just cleaned up a bit. And I will send it to you then.
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
12/08/12 09:36 AM (13 years ago)
so many great hints and tips in this thread.... thanks for bringing this thread up Mack - I might have some specific questions for you on this next week if you're around? sometimes it helps more to learn from others that are learning to really understand this. I'm starting to get close with my custom array....
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
12/08/12 09:51 AM (13 years ago)
Hi atrain You are welcome to ask questions about this!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.