Better Faster Way?

Discussion in 'iOS Programming' started by iLoveDeveloping, Feb 28, 2010.

  1. iLoveDeveloping macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #1
    Hey,

    I'm just making a new App and it has a huge 130 pages of content! Now so far I have been using ViewControllers for each page but like 20 pages in and I am going crazy... This will take forever...!

    Is there any other ways to present a new page from only a handful of ViewControllers? Or even one? I have never done anything like that so could anyone give me advice on how to do it? or any relevant links?

    Thanks guys...
     
  2. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #2
    Depends, What are all of these 130 Viewcontrollers doing?
     
  3. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #3
    Why not use only a couple of viewcontrollers and change what is being viewed by pulling records out of a database?
     
  4. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #4
    They are showing a text view and 1 small image on each view.


    Yes, how would i do that? I have never tried a database App before... Are there any samples of this working?
     
  5. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #5
  6. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #6
    You don't even need a data base for that. If each different image/text your accessing is retrieved by a button then why not just say:
    HTML:
    .h
    @interface VIEWCONTROLLERNAME : UIViewController {
         IBOutlet UIImageView *image;
         IBOutlet UITextField *TextField;
    }
    -(IBAction)Button1;
    
    @end
    
    HTML:
    .m
    
    -(IBAction)Button1 {
         image = [UIImage imagenamed:@"image1"];
         TextField.text = @"Text for button 1 to show";
    }
    
    and so on and so forth for
     
  7. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #7

    That works really well, Thanks!!

    TextField.text is working great but With
    Code:
         image = [UIImage imagenamed:@"image1"];
    i'm getting a warning and that's crashing the programme:

    warning: 'UIImageView' may not respond to '+imageNamed:'
     
  8. bindle macrumors regular

    Joined:
    Aug 29, 2008
    #8
    ..because 'image' is not a UIImage, it's a UIImageView.
    Maybe rename the variable to 'imageView" so it makes more sense.

    But you want to use setImage: to set the UIImage in the UIImageView:

    Code:
    
    [image setImage:[UIImage imageNamed:@"image1"]];
    
    
     
  9. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #9
    Perfect thanks, i had a feeling that line was missing something.. Very Thankful, i will see does that all work now! God that would save so much space!! 1 view instead of 113!!! You saved my life... :) not to mention everyone's iPhone space!!!
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Conceptually, what you're dealing with here is typical model-view-controller programming. Take this as an opportunity to go learn more about that.
     
  11. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #11
    sorry

    sorry, i forgot the .image

    it should read:
    HTML:
    image.image = [UIImage imagenamed:@"image1"];
     
  12. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #12
    I am, i'm reading some documentation about all this now! Very interesting.. (Well, for people like us... :D)

    Yes its all working now, but i still have to put all that text into the actual code, remember is it 130 pages of text! Is there some way i could possibly use a .Txt file in place of

    Code:
     TextField.text = @"Text for button 1 to show";

    Like link it so when the button is pushed, it goes to "FirstScreen.txt" file and displays what is in that text file on screen and so on... Therefor simply leaving me just to write up 130 .txt files and add them to resources?
     
  13. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #13
    Yes, you can do this:
    HTML:
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TEXTFILENAME(DO NOT PUT THE .txt)" ofType:@"txt"];
    if (filePath) {
         NSString *text = [NSString stringWithContentsOfFile:filePath];
         TextField.text = text;
    }
    	
     
  14. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #14
    Wooo thanks, i seen that in the documentation but wasn't sure if i could use NSString, must read some more about that!

    Thanks guys for all the help!
     
  15. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #15
    Just a note, but you can put the extension there if you want, but don't put it in both places. Do one or the other.
     
  16. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #16
    Just one problem i am running into now! Something i didn't consider when i started!
    Am, might seem silly this deep into the App but, how do i get the same button to change each view?

    I have made Button1, which shows 1.txt and Image1.png when hit!
    But what happens when i need to show 2.txt and Image2.png on the same View?
    I assume I'll have to do this programmatically because in IB i'm not seeing a solution, surely one button can't do 130 things...!?
     
  17. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #17
    Depends, you may have to make 130 buttons BUT i really depends what your doing? Can you give us a vague idea of what you doing? Don't worry nobody's going to steal your idea.

    Personally as it is i have enough apps i have to work on.
     
  18. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #18
    But how do i put 130 buttons on one View?
    Is it possible that when i hit the button to change the .txt and .png it can also change the buttons so each time there is basically a fresh screen?

    I'm making a simple app that just has some simple content (TextView and 1 very small image per page) I load up page two, then i want to hit a button so the user can go through each text and image one by one or go backwards, so i need to have the 130 different text fields and images set up to cycle through each time the button is pressed.
     
  19. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #19
    ok so it looks like 130 buttons, and you would do that by creating a UIScrollView, Scroll views can be confusing. What you want to do is make the view and scroll view as BIG AS POSSIBLE so that the ends of the view touch the sides of your screen. Then put all of the buttons in and hook them up, then look at the bare minimum pixel dimensions needed for your scroll view (you will need those later)and then size it back to 320x480 this will cut off most of your buttons from site for now. Then go into your .m (after you set up the scroll view and buttons in your .h) and write this in the viewDidLoad method
    HTML:
    ScrollViewName.contentSize = CGSizeMake(x, y);
    WAIT remember the dimensions of the bare minimum for the scroll view i told you about? replace the "x" with the width and the "y" with the height and that should work.

    I Know it's confusing.
     
  20. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #20
    You don't need to make 130 different buttons, you can generate them dynamically, just the same as the rest of the content. All you need to do is keep track of what page you're on, you should be able to determine all the content for each page programatically. When the Next button is pressed, you add one to the pageNumber and update your content, and vice versa.
     
  21. iLoveDeveloping thread starter macrumors regular

    Joined:
    Sep 24, 2009
    Location:
    Ireland
    #21
    How would i go about doing that?
     
  22. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #22
    Code:
    // Let's say this is the beginning of your Next button code.
    // Previous will be almost identical.
    
    // This adds one the the variable pageNumber.
    ++pageNumber;
    
    // This operation dynamically creates a string in the form "3.txt"
    // substituting the currentPage number where 3 is. 
    NSString *textFile = [NSString stringWithFormat: @"%d.txt", pageNumber];
    
    // Here is another one for the image.
    NSString *imageFile = [NSString stringWithFormat: @"Image %d.png", pageNumber];
    
    // At this point you can pretty much just use the same code you already have,
    // But substitute in these dynamic strings above with the ones you hard coded in.
    
     

Share This Page