Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iLoveDeveloping

Suspended
Original poster
Sep 24, 2009
596
2,366
Ireland
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...
 
Why not use only a couple of viewcontrollers and change what is being viewed by pulling records out of a database?
 
Depends, What are all of these 130 Viewcontrollers doing?

They are showing a text view and 1 small image on each view.


Why not use only a couple of viewcontrollers and change what is being viewed by pulling records out of a database?

Yes, how would i do that? I have never tried a database App before... Are there any samples of this working?
 
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
 
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


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:'
 
..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"]];
 
..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"]];

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!!!
 
Conceptually, what you're dealing with here is typical model-view-controller programming. Take this as an opportunity to go learn more about that.
 
Conceptually, what you're dealing with here is typical model-view-controller programming. Take this as an opportunity to go learn more about that.

I am, i'm reading some documentation about all this now! Very interesting.. (Well, for people like us... :D)

sorry, i forgot the .image

it should read:
HTML:
image.image = [UIImage imagenamed:@"image1"];

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?
 
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;
}
 
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;
}

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!
 
-(IBAction)Button1;

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.

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...!?
 
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.
 
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.

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.
 
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.
 
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.
 
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.

How would i go about doing that?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.