Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I would do it. (I've changed the name of a few instance variables: <code>currentImage</code> sounds like it could be a pointer to an image (UIImage *) rather than just an integer value; adding <code>Index</code> to the end makes that more clear. It may be obvious now that it's an integer, but when you revisit this code (or other code you write) in a month, it may be less obvious; or maybe that's just me)...</p> <p><code>MDSlideshowController.h</code>:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class MDBilboBaggins; @interface MDSlideshowController : NSObject { IBOutlet UIImageView *imageView; NSArray *imageArray; NSUInteger currentImageIndex; BOOL someOtherVariable; MDBilboBaggins *bilboBaggins; // keep adding more instance variables as needed } - (IBAction)previous:(id)sender; - (IBAction)next:(id)sender; @end </code></pre> <p><code>MDSlideshowController.m</code>:</p> <pre><code>#import "MDSlideshowController.h" #import "MDBilboBaggins.h" // you could perhaps define currentImageIndex here, but see notes below: // NSUInteger currentImageIndex = 0; @implementation MDSlideshowController // `currentImageIndex` is automatically initialized to 0 during init // `someOtherVariable` is automatically initialized to 0 (NO) during init -(void) viewDidLoad { imageArray = [[NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], nil] retain]; [imageView setImage:[imageArray objectAtIndex:currentImageIndex]]; } - (IBAction)previous:(id)sender { if (currentImageIndex == 0) { currentImageIndex = [imageArray count] - 1; } else { currentImageIndex--; } [imageView setImage:[imageArray objectAtIndex:currentImageIndex]]; } - (IBAction)next:(id)sender { if (currentImageIndex + 1 &gt;= [imageArray count]) { currentImageIndex = 0; } else { currentImageIndex++; } [imageView setImage:[imageArray objectAtIndex:currentImageIndex]]; } @end </code></pre> <p>Basically, you put instance variables right underneath the ones you've already defined. They can be of almost any type. You can use the types Cocoa Touch knows about, or classes you make yourself. In this example, I said that there was a special class named <code>MDBilboBaggins</code> by using the <code>@class MDBilboBaggins</code> statement. Then, I add the <code>#import "MDBilboBaggins.h"</code> part in the .m file: this can help speed up compile times.</p> <p>As I mentioned in the comment, you could perhaps define the <code>currentImageIndex</code> variable inside the .m file, however, it would be a static variable that is common to, and shared, by all instances of the class. This can be useful in some situations, but create issues in others. For example, imagine we have 2 slideshow controllers, each with different images created and showing slides in 2 different windows. If they were modifying a shared <code>currentImageIndex</code> variable, they'd mess each other up if you would switch between them and start clicking Previous and Next indiscriminately. That's why in this case it might make more sense to just make it an instance variable by defining it other your other instance variables in the .h file.</p> <p>[EDIT] Regarding the <code>:(id)sender</code> parameter: in this example, it wasn't used at all, I generally do it out of habit, since in some circumstances, it can save a lot of code, and simplify things dramatically. For example, say you had 9 different buttons and you wanted each button to load a specific image (or perform a specific operation). Now, you could define 9 separate methods like <code>- (IBAction)button1Clicked;</code>, or you could do it the easy way and just define a single <code>- (IBAction)loadImage:(id)sender</code> method. In your nib file, you would give each button a different tag (an <code>NSInteger</code> value), like 1 − 9. Then in your single method you could do this:</p> <pre><code>- (IBAction)loadImage:(id)sender { NSInteger buttonTag = [(NSButton *)sender tag]; [imageView setImage:[UIImage imageNamed: [NSString stringWithFormat:@"image%ld.png",buttonTag]]]; } </code></pre> <p>In this case, <code>sender</code> is the object that sends the message, which would be the button that was clicked on. Though admittedly a contrived example, by providing that one additional parameter, I probably saved 100 lines of needless code and complexity of having 9 separate methods.</p> <p>[EDIT #2] Replaced the one pseudo-coded (written in Safari) <code>next:</code> method with actual code from one of my apps that I know works.</p> <p>Hope this helps...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload