Reputation: 3113
I am trying to make a UIButton display an image and am getting the Title error.
Also I would like to know why ()
is needed on BSViewController ()
as it was created by XCode?
//
// BSViewController.m
#import "BSViewController.h"
@interface BSViewController () // Why the "()"?
@end
@implementation BSViewController
- (IBAction) chooseImage:(id) sender{
UIImageView* testCard = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ipad 7D.JPG"]];
//Property 'window' not found on object of type 'BSViewController *'
self.window.rootViewController = testCard;
[self.window.rootViewController addSubview: testCard];
testCard.center = self.window.rootViewController.center;
NSLog(@"chooseImage");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// BSViewController.h
#import <UIKit/UIKit.h>
@class BSViewController;
@interface BSViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
IBOutlet UIButton* chooseImage;
}
- (IBAction) chooseImage:(id) sender;
@end
Upvotes: 1
Views: 7756
Reputation: 31745
This line:
self.window.rootViewController = testCard;
Is an attempt to assign an imageView object pointer to an existing viewController object pointer. You should have got a compiler warning about that. Then on the next line you effectively try to add it to itself as a subview, that probably kicks up a warning as well.
The () indicate a category extension to a class. It's an extension to your public interface which allows you to declare entities that should be private to the class. You should put most of your interface here, just keep things in your .h @interface that need to be public.
Your BSViewController class does not have a property called window
so you cannot refer to it as self.window
. But under normal circumstances you should be able to get a reference to your window like this:
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
[window.rootViewController.view addSubview: testCard];
However, if you just want to put testCard into your instance of BSViewController you don't need to do that. You just need a reference to your current instance's view:
[self.view addSubview:testCard];
Upvotes: 12