adding UIImageView to UIScrollView throws exception cocoa touch for iPad - objective-c

I am a noob at OBJ-C :)
I am trying to add a UIImageView to a UIScrollView to display a large image in my iPhone app.
I have followed the tutorial here exactly:
http://howtomakeiphoneapps.com/2009/12/how-to-use-uiscrollview-in-your-iphone-app/
The only difference is that in my App the View is in a seperate tab and I am using a different image.
here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cheyenne81"]];
self.imageView = tempImageView;
[tempImageView release];
scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imageView;
}
and:
#interface UseScrollViewViewController : UIViewController<UIScrollViewDelegate>{
IBOutlet UIScrollView *scrollView;
UIImageView *imageView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) UIImageView *imageView;
#end
I then create a UIScrollView in Interface Builder and link it to the scrollView outlet. Thats when I get the problem. When I run the program it crashes instantly.
If I run it without linking the scrollView to the outlet, it will run (allbeit with a blnk screen).
The following is the error I get in the console:
2010-03-27 20:18:13.467 UseScrollViewViewController[7421:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x4a179b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key scrollView.'

Try to add
#synthesize imageView;
into the #implementation of UseScrollViewViewController.

As there was no accepted answer solving the problem I figured I post my answer which solved my similar problem.
As tab bar is being used, you should also check the Tab Bar Controller's attributes in IB. Most probably the type for your view controller (which is under Tab Bar Controller item, which is I guess in MainWindow.xib) is UIViewController. Change it to your specific controller.

First, the obligatory "the iPad SDK is still under NDA, so you shouldn't be posting this question here, and we can't help you" response.
Now If this were and iPhone app I would say check in the interface builder: click on "File's owner" and in the inspector, make sure that the class is set to be the class you are using to load the nib.

Related

UIImageView not showing up

I have a UIImageView with property, it's connected in the storyboard and in viewDidLoad I have set it's image. I first create an image then assign it to the image of the UIImageView.
-(void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [[UIImage imageNamed:#"image"]resizeableImageWithCapInsets:UIEdgeInsetsMake(28.5, 5, 6.5, 5)];
[self.imageView setImage:image];
}
I've made sure numerous times that the UIImageView is connected in the IB and it's showing up as connected. I have a UICollectionView over it, but it doesn't extend high enough to cover it all and the UICollectionView background color is set to clear. Thanks in advance for the help.
I suppose you added the IBOutlet UIImageView * imageView in your .h and then linked it in the Interface Builder, BUT did you add the property of the imageView? Like this:
#property (nonatomic, retain) IBOutlet UIImageView * imageView;
And then in your .m file, under the #implementation you need to add #synthesize imageView;
and set the image to your imageView using only: [imageView setImage:image]; (without the self.).
(You can also skip the #synthesize and just use [_imageView setImage:image];)
The #property allows you to edit the properties of the object, like backgroundColor, alpha, image, etc.

iPhone back button refuses to work after selecting an image with the UIImageViewController? [duplicate]

This question already has answers here:
iPhone - UIImagePickerControllerDelegate inheritance
(2 answers)
Closed 9 years ago.
So I am writing this iPad app that starts on a main screen and then from there you can go to a settings page. In the settings page you have the ability to select a picture from the photo album using a UIImagePickerController in a popover view.
If I go to the settings page and then press the back button to return to the main page everything works as expected. But if I go to settings and pick an image the back button on the page will not let me go back to the main page.
The popover and UIImagePickerController seem to be working fine so I do not know what is causing this. Here is my code for the UIImagePickerController.
- (IBAction)imagePick1:(id)sender {
pickerController = [[UIImagePickerController alloc] init];
[pickerController setDelegate:self];
[pickerController setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[pickerController setAllowsEditing:NO];
popoverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[popoverController setDelegate:self];
[popoverController presentPopoverFromRect:[[self imageButton1] frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
...
- (void)imagePickerController:(UIImagePickerController *)pickerController1 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
_image1.image = image1;
[popoverController dismissPopoverAnimated:YES];
}
When I try to press the back button the app freezes and it won't respond to any more commands. What am I doing wrong?
Edit: I ran it again and this is the error I got in the log when pressing the back button.
-[__NSCFType dismissPopoverAnimated:]: unrecognized selector sent to instance 0x7128cd0
Also here is the beginning of my header file.
#interface ViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate>{
UIPopoverController *popoverController;
}
#property (nonatomic, retain) UIPopoverController *popoverController;
Possible duplicate. See here
Have you declared the delegate in the header file as well like below? Please also make sure your delegates conforms to the UINavigationControllerDelegate as well, as the UIImagePickerControllerDelegate directly inherits from UINavigationControllerDelegate, however all the UINavigationControllerDelegate methods are optional.
#interface MyViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
edit: Based on your edit, it appears that you need to also add the "UIPopoverControllerDelegate" to your header file. You also need to add [popoverController setDelegate:self] after your allocation.
edit 2: Ok now you need to declare this method as well in your implementation (.m) file.
and return yes.
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
return YES;
}
edit 3: Why are you are setting dismissPopoverAnimated to NO and releasing popoverController while it is still showing?
[popoverController dismissPopoverAnimated:NO];
[popoverController release];
try setting the dismissPopoverAnimated to YES and remove the release for now. By the way, is popoverController retained as a property in your header file? Are you using ARC? if you are using ARC you should not be calling release on the popoverController.
edit 4:
try modifying the header to this:
UIPopoverController *_popoverController;
#property (nonatomic, retain) UIPopoverController *popoverController
and add #synthesize popoverController = _popoverController; to your implementation file.
now start replacing all the instances of "popoverController" with "_popoverController" in your implementation file.
Also replace [popoverController dismissPopoverAnimated:YES]; with
if(_popoverController != nil)
[_popoverController dismissPopoverAnimated:YES];
let me know the results below.

Why won't this code allocate a subView?

This is my code where I'm trying to create a subView... I'm using XCode4 with StoryBoards. The app is crashing on the 2nd line where it is allocating subView with EXC_BAD_ACCESS. vFrame has valid content. What is wrong with this? (I'm using XCode4 with Storyboards, btw).
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect vFrame = CGRectMake(60,100,200,200);
subView = [[UIView alloc] initWithFrame:vFrame];
subView.backgroundColor = [UIColor redColor];
[self.view addSubview: subView];
}
UPDATE: definition for subView:
#interface PreferencesViewController : UIViewController {
UIView *subView;
}
#property (nonatomic, retain) UIView *subView;
#end
That really should work. Was your view controller (PreferencesViewController) properly alloc'd and init'd? Did you #synthesize your subview?
Although it shouldn't matter, you could try using floats for the CGRect (add a .0 to the end of each).
Try moving your sub view instantiation code to the method viewWillAppear of your main view. This guarantees everything has been initialized.
The problem was the UIViewController was farkled...don't know how, but once it was replaced, it worked like a champ! Thank you all for your responses...

How to display an image using UIView in XCode?

Absolutely beginner question. I am trying to make an app which will switch an array of images by swiping the screen sideways. So, as vanilla case of that I was trying to display an image first and then think about how to switch between images using an action. I am using XCode 4.2.
So, here's what I have so far. I have added a UIImageView to my storyboard, then Ctrl+dragged into the ".h" file to create an Outlet and it looks something like this:
SwitchViewController.h
#import <UIKit/UIKit.h>
#interface SwitchViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
And then in the ".m" file, I am trying to set an image to the UIImageView in the DidLoad method.
SwitchViewController.m
#import "SwitchViewController.h"
#implementation SwitchViewController
#synthesize imageView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *plate1 = [UIImage imageNamed:#"plates1.tif"];
[imageView setImage:plate1];
}
- (void)viewDidUnload
{
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
return YES;
return NO;
}
#end
Now, if I try to run this, there are no compilation errors. But the program halts saying, Thread 1:Program received signal "SIGABRT"
Referring to your actual problem, you're getting this exception being thrown:
2012-01-26 22:04:40.728 Switch-a-Switch[548:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SwitchViewController 0x6840660> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UIImageView.'
It would appear that you've wired up the UIImageView to a property called UIImageView rather than imageView. Go and redo the connection in Interface Builder and make sure you drag from the imageView property to your UIImageView instance.
UIImage *plate1 = [UIImage imageNamed:#"plates1.tif"];
[imageView setImage:plate1];
should be kosher in iOS and 64 Bit (non-fragile ABI environments, as the iVar will be synthesized), I think it is much better form to include the ivar in the interface....
#interface SwitchViewController : UIViewController
{
UIImageView *imageView;
}
The thing that you should do to track this down is to turn on zombies and enable an exception breakpoint.
afew things there. (now im only learning myself so may be wrong but here goes)
when you use the #property u should use the self.imageView setter to assign to it. so [imageView setImage:plate1] would become self.imageView.image = plate1. and change it from weak to retain in the .h. and in the viewdidunload do self.imageView = nil. also i think the swiping of images effect your trying to achieve here might be better suited to a horizontal scrollview. and u can turn on paging or something for that.
hope that helps and didnt send u completely wrong direction :P
I think you may be going about this the wrong way. If you want to side swip to go between images, you should use a UIScrollView with paging enabled. A quick google came up with this.
Hope that helps

Why isn't my UIButton responding to touches?

I'm sure I'm overlooking the obvious as I've got countless working buttons...but...for whatever reason this one is not cooperating...
I've added a UIButton (Rounded Rect) to a UIView subclass (DialogView) which is a subview of my view controller's view. This subview is created almost entirely in IB. I've wired up the button to (IBAction)okButtonPressed:(id)sender in IB to Touch Up Inside and created a corresponding method in DialogView. However when I "touch" this button it doesn't trigger the method. userInteractionEnabled is true for the VC's view, DialogView and the UIButton.
Thinking maybe initWithCoder had to do some frame manipulation or something I added the following which successfully logs to console.
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(#"DialogView initWithCoder called");
}
return self;
}
In further exploration I wired up an IBOutlet to the button and then if I try to change the titleLabel from the view controller I notice that it get's severely truncated. Default text of say "Press Me!" set in IB displays fine when view is first drawn. But if I change the text...
self.DialogView.okButton.titleLabel.text = #"Not Working";
...it gets truncated to "N..."
Dunno if this is related. Probably...
Anyone see what I've screwed up here?
Edit (adding code related to showing UIButton):
From the View Controller:
self.DialogView = [[[NSBundle mainBundle] loadNibNamed:#"DialogView" owner:self options:nil] objectAtIndex:0];;
self.DialogView.myVC = self;
self.DialogView.backgroundColor = [UIColor clearColor];
self.DialogView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
self.DialogView.nameLabel.text = loan.fullName;
self.DialogView.noteLabel.text = loan.summaryOfLoan;
self.DialogView.amountLabel.text = [currencyFormatter stringFromNumber:loan.originalAmount];
self.DialogView.alpha = 0.0;
[self.view addSubview:DialogView];
The UILabels all displaying as expected. As is the problem UIButton. I can see it I just can't interact with it!?!
DialogView's interface:
#class MyViewController;
#interface DialogView : UIView {
IBOutlet UILabel *nameLabel, *noteLabel, *amountLabel;
IBOutlet UIImageView *arrowView;
IBOutlet UIButton *okButton;
MyViewController *myVC;
}
#property (nonatomic, retain) UILabel *nameLabel, *noteLabel, *amountLabel;
#property (nonatomic, retain) UIImageView *arrowView;
#property (nonatomic, assign) MyViewController *myVC;
#property (nonatomic, retain) UIButton *okButton;
- (IBAction)okButtonPressed:(id)sender;
#end
And DialogView's implementation:
#import "DialogView.h"
#import "MyViewController.h"
#implementation DialogView
#synthesize nameLabel, noteLabel, amountLabel, arrowView, okButton;
#synthesize myVC;
- (void)dealloc {
[nameLabel release];
[noteLabel release];
[amountLabel release];
[arrowView release];
[okButton release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(#"DialogView initWithCoder called");
}
return self;
}
- (IBAction)okButtonPressed:(id)sender {
NSLog(#"pressed DialogView OK button");
[self.myVC.navigationController popViewControllerAnimated:YES];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
#end
I thought that we should use -setTitle:forState: in order to set button's title ?
An other thought, did you check that the button's frame is not CGRectZero ? And by the way, all the frames for the view in the hierarchy ? And check that one superview in the hierarchy is not user interaction disabled ?
And, I think imageView does not respond to touches, do you have one in your code ?
I was just having more or less the same problem and I found that my containing view did not have "User Interaction Enabled".
Hope this helps.
Do you maybe have two buttons on top of one another? Change the IB project window to the detail view and see if your view has more buttons than you are expecting. Maybe you've wired up a button that's not actually getting the press you're expecting.