views:

154

answers:

2

I have a UISegmentedControl and I have some photos, my initial aim is when ever I click on a particular segment one particular image should appear on the view.

For example: If i have four segments and four images then upon each segment I click I must see a image.

Here I have taken an NSArray and Uploaded all these images using this particular below code:

NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"f.ppg"], [UIImage imageNamed:@"t.ppg"....etc ], nil];

and after this I want to attach each particular image to a segment index. So here is the code which I wrote.

-(IBAction) segmentActionChanged:(id)sender
{
    int segment = mSegment.selectedSegmentIndex; 
    mImageView.image=[images objectAtIndex:segment]; 
}

While compiling the application I am not getting any Images displayed and it is quiting abruptly.

Any help regarding this.

+1  A: 

To Answer questions 1 and 3 (I think). to display an image, you will need a UIImageView somewhere. a simple way to do this would be to create an method such as this.

    //somewhere in viewDidLoad perhaps?
    //title array is an array of strings to use as the label on each section of your control
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];

    //this sets the segmentAction: method as the selector for your particular UISegmentedControl 
//(so when its value is changed, segmentAction: gets called)
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    //the segmentAction: method
    - (IBAction) segmentAction:(id)sender
    {
    //This assumes you have declared the imageView elsewhere, as well as the array and that you  
 //are using Interface Builder to create/hookup the segmentControl. Basically
        myImageView.image = [myImageArray objectAtIndex:sender.selectedSegmentIndex];
    }

A few Notes: this does not take advantage of lazy loading, since you are creating and holding all of your images in an array prior to showing them. I would suggest creating an instance variable for the UISegmentedControl so you can access it anywhere also. Disclaimer: this code is not complete and assumes some initializations.

To answer question number two:A UIImagePickerController is used when dealing with the phone's/devices camera or saved photos album. It is covered in the documentation.

Jesse Naugher
I understood what you wrote.Initially I tried your it didn't worked and then I tried my own which is shown above(modified my question). but I am getting an error while loading images in to the Array saying that "Missing sentinel in Function call"...Can you help me in this.
adusum
Thanks for answering my doubts.
adusum
I solved this Missing sentinel in Function call error.But application is qutting all of the sudden with out showling the images in the UIImageView.
adusum
A: 

Looks like you're missing a retain.

tc.
you mean @property i did it in .h file.I got the result if I load all the images individually using UIImage.But I am not gettig using NSArray and not able to display the images.
adusum
Simply adding a property won't do any good; you still need to write `self.images = [NSArray ...]` which uses the accessor (which does the retain).
tc.