AppDelegate create NSView, draw NSImage to NSView - objective-c

I'm currently trying to create a simple app that accesses a folder and randomly loads images and displays them in its window.
As expected I'm struggling right away.
I have to files:
AppDelegate.h
AppDelegate.m
.h is empty, .m contains all the code.
#import "AppDelegate.h"
#interface AppDelegate ()
#property (strong) NSImage *image;
#property (weak) IBOutlet NSWindow *window;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSRect frame = [_window frame];
NSView *view = [[NSView alloc] initWithFrame: frame];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"pathtoimage" ofType:#"jpg"];
_image = [[NSImage alloc] initWithContentsOfFile:imagePath];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
So how do I now set the view to my window and how do I then draw the image to the view?
EDIT:
Changed code to this, still nothing displayed except an empty window.
#import "AppDelegate.h"
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSRect frame = [_window frame];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"/Users/Me/Documents/Me/Art/picturename" ofType:#"png"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imagePath];
NSImageView *myImage = [[NSImageView alloc] init];
[myImage setFrame:frame];
[myImage setImage:image];
[self.window.contentView addSubview:myImage];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
EDIT2:
#import "AppDelegate.h"
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSRect frame = [_window frame];
//NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"doorTech" ofType:#"jpg"];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:#"/Users/Adrian/Documents/Adrian/Art/desertMovie.jpg"];
if (image == nil) {
NSLog(#"Image is nil");
} else {
NSLog(#"Image is not nil");
}
NSImageView *myImage = [[NSImageView alloc] init];
[myImage setFrame:frame];
[myImage setImage:image];
[self.window.contentView addSubview:myImage];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end

Related

How to set values of subclassed UIView properties when loading subclass from a xib

I have a subclassed UIView called BannerHeaderView, and designed the way I want it layed out in an xib. I have set the xib class to BannerHeaderView. I have a UILabel and a UIImageView which I have connected in IB.
The problem is that when I go to set the values of these subViews after initialising my BannerHeaderView in a ViewController, the label and image view are null. I have tried setting them in init also.
Code for BannerHeaderView.h:
#interface BannerHeaderView : UIView
#property (strong, nonatomic) IBOutlet UIImageView *bannerImage;
#property (strong, nonatomic) IBOutlet UILabel *headerLabel;
-(void)setTitle:(NSString *)title;
-(void)setImageFile:(PFFile *)imageFile;
#end
Code for BannerHeaderView.m:
#implementation BannerHeaderView
#synthesize headerLabel, bannerImage;
-(id)init{
self = [super init];
if (self) {
[self customInit];
}
return self;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self customInit];
}
return self;
}
-(void)customInit{
UIView *contentView = [[[NSBundle mainBundle] loadNibNamed:#"BannerHeaderView"
owner:self
options:nil] objectAtIndex:0];
[self addSubview:contentView];
contentView.frame = self.bounds;
}
-(void)setTitle:(NSString *)title{
NSLog(#"HEADER = %#", headerLabel);
headerLabel.text = title;
}
And I am creating the BannerHeaderImage in my ViewController like so:
bannerHeader = [[BannerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, bannerHeight + segmentedHeaderHeight)];
[bannerHeader setTitle:#"I AM THE NEW TITLE"];
The log tells me that the headerLabel is null.
Questions:
How do I set the values of these subviews?
Should I just design the layout of this programatically?
Thanks for your time.
P.S. I have spent hours researching this and I can't find a solution. I did find similar questions on SO but none with suitable answers to enable me to solve my problem...
Please set the class BannerHeaderView to File's Owner like this

AVPlayer plays sound but view is not shown in mac application

I'd like to play local movie in AVPlayer on macintosh app. I've created simple code but movie doesn't want to show played movie. It plays sound of the movie but there is no view visible. What am I missing?
It's my first playing simple app.
My code:
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSView *containerView = [[NSView alloc] initWithFrame:CGRectMake(0.0f, 0, 390.0f, 400.0f)];
NSString *videoPath = [[NSBundle mainBundle] pathForResource: #"Fatality.mp4" ofType:nil];
NSLog(#"%#", videoPath);
NSURL *fileURL = [NSURL fileURLWithPath:videoPath];
self.player = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];
[self.player play];
}
#end
And AppDelegate:
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet MasterViewController *masterViewController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
self.masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.masterViewController.view];
self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
You have to add your containerView as a subview of the master view controllers view
[self.view addSubView:containerView];
A trick I often do while debugging is to set the background colour of a view I am interested in to something bright (red, yellow, cyan....) and then make sure that the view shows up where I expect it to be.

Trying to fix subview flipping and now subview buttons no longer work?

I have a view which includes two subviews. I had it working so that only one subview was shown at a time and each subview had a button and when the button was clicked the subview would flip over and the next subview would appear. The problem was that it appeared as though the entire view was flipping. After reading on this site about how to solve the problem I attempted to add the subviews to a container and flip that instead. However now, although my first subview is showing up when I press the button it no longer flip. It doesn't do anything. I put a log statement in the method which flips the subviews, as well as a breakpoint and as far as I can tell it no longer gets called. I'm very new to xcode and objective c and delegates and I have no idea how to proceed. Any help would be appreciated. Thanks.
I have included the relevant code here:
The header for the ViewController
#interface ExerciseViewController : UIViewController<ExerciseSubViewDelegate>
//stuff for subviews
#property (nonatomic, strong) ExerciseSubViewImage *subViewImage;
#property (nonatomic, strong) ExerciseSubViewText *subViewText;
#property UIView *panel;
#end
This is the code for the ViewController:
#interface ExerciseViewController ()
#end
#implementation ExerciseViewController
#synthesize subViewImage, subViewText;
- (void)viewDidLoad
{
self.subViewImage.delegate = self;
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
}
-(ExerciseSubViewImage *)subViewImage
{
if (!subViewImage)
{
CGRect subViewImageFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
self.subViewImage = [[ExerciseSubViewImage alloc] initWithFrame:subViewImageFrame];
[_panel addSubview:subViewImage];
}
return subViewImage;
}
-(ExerciseSubViewText *)subViewText
{
if (!subViewText)
{
CGRect subViewTextFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
self.subViewText = [[ExerciseSubViewText alloc] initWithFrame:subViewTextFrame];
self.subViewText.backgroundColor = [UIColor blueColor];
[_panel addSubview:subViewText];
}
return subViewText;
}
-(void)exerciseSubViewImagePressed
{
[UIView transitionWithView:_panel
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[subViewImage removeFromSuperview];
[_panel addSubview:subViewText];
}
completion: nil];
//This is how I did it before I added the container
/*[UIView transitionFromView:subViewImage
toView:subViewText
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
self.subViewText.delegate = self;*/
NSLog(#"Ipushedtheimage");
}
-(void)exerciseSubViewTextPressed
{//I haven't updated this yet
[UIView transitionFromView:subViewText
toView:subViewImage
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
self.subViewImage.delegate = self;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
subViewImage = nil;
subViewText = nil;
}
#end
This is the code for the delegate
#import
#protocol ExerciseSubViewDelegate <NSObject>
-(void) exerciseSubViewImagePressed;
-(void) exerciseSubViewTextPressed;
#end
I am also added the code for the first subview:
#import
#import "ExerciseSubViewDelegate.h"
#interface ExerciseSubViewImage : UIView
#property (nonatomic, strong) UIButton *button;
#property (nonatomic, assign) id<ExerciseSubViewDelegate>delegate;
#property (strong, nonatomic) UIImageView *exerciseImageView;
#end
#import "ExerciseSubViewImage.h"
#import "UIImage+animatedGIF.h"
#implementation ExerciseSubViewImage
#synthesize button;
#synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Initialization code
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectMake(50,200,100,35);
self.button.frame = buttonFrame;
[self.button setTitle:#"Image"forState:UIControlStateNormal];
[self.button addTarget:self
action:#selector(buttonTouched)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.button];
_exerciseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,20,160,158)];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"AppleLogo" withExtension:#"gif"];
_exerciseImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];
[self addSubview:self.exerciseImageView];
}
return self;
}
-(void)buttonTouched
{
NSLog(#"imagebuttonpressed");
[self.delegate exerciseSubViewImagePressed];
}
Again, any help would be appreciate. I know I'm probably just not understanding something simple.
Ok. This took me all weekend but I finally figured it out on my own. I thought I would shere the answer here in case anyone else ever has a similar problem. After trying several other approaches I finally went back to the approach I used here and started inserting a whole bunch of NSLogs to determine the order that every thing was executing in. What I finally ended up doing was changing this: (all in the top ViewController)
self.subViewImage.delegate = self;
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
to this:
//create panel
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, s self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
//Set the subview delegates
self.subViewImage.delegate = self;
self.subViewText.delegate = self;

iOS / Objective-C: UIView delegates not called

i´ve got a uiview in a scrollview which is in a viewcontroller and it seems that the delegates are not called because i added them to the uiview.
Is it possible to solve this problem? (I want to keep the delegates in the uiview because it seems to be cleaner code)
Maik
EDIT:
in .h file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface RHCHuntingReportViewPageTwo : UIView <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
in .m file:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *pin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
pin.image = [UIImage imageNamed:#"map_pin.png"];
pin.draggable = NO;
pin.canShowCallout = YES;
return pin;
}
I added the delegate and outlet correctly in storyboard.
edit 2:
in .m file:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
RHCAnnotation *annotation = [[RHCAnnotation alloc] init];
annotation.title = #"title";
annotation.subtitle = #"subtitle";
annotation.image = [UIImage imageNamed:#"map_tipp.png"];
annotation.coordinate = CLLocationCoordinate2DMake(57.242465, 2.533579);
mapView.region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 250, 250);
[mapView addAnnotation:annotation];
}
return self;
}

How to send array to subview and then print into label?

Working over storyboard.
I've created View Controller (storyboard) and then in middle of content added subview from xib file.
I want to add xib (UIView) like as subview into ViewController and send object with data and print that data into label but I don't know how.
Here is my code.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIView *subView;
}
#property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
#end
ViewController.m
#import "OfferViewController.h"
#import "OfferLocation.h"
#interface OfferViewController ()
#end
#implementation OfferViewController
#synthesize subView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
OfferLocation *location = [[OfferLocation alloc] initWithFrame:CGRectZero];
[self.subView addSubview:location];
}
...
#end
and here is subview:
OfferLocation.h
#import <UIKit/UIKit.h>
#interface OfferLocation : UIView{
UIView *view;
UILabel *locationLabel;// here is that label taht I want to print into
}
#property (nonatomic, retain) IBOutlet UIView *view;// connected over IB
#property (nonatomic, retain) IBOutlet UILabel *locationLabel;
#end
OfferLocation.m
#import "OfferLocation.h"
#implementation OfferLocation
#synthesize view, locationLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
locationLabel.text = #"some text"; //this is not working
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"OfferLocation" owner:self options:nil];
UIView *tempView = [subviewArray objectAtIndex:0];
[self addSubview:tempView];
}
return self;
}
subView never seems to be initialized and loaded to the view or presented. Therefore you cannot see it.
Try calling:
self.view = self.subView // or subView; // or [self.view addSubview:self.subView // or subview];
or something similiar
Then declare a UILabel and initialize it with whatever text you want and add it to subview:
UILabel *label = [[UILabel alloc] initWithString:#"StackOverflow rocks!"];
[self.subView addSubview:label];
// or [self.view addSubview:label];
Differing on how you set subView.
What is this data you want to set on label's text? If it is a data type use the specifiers in the string.
%i // for int
%# // for string or object adress
%c // for char
%f // for float
etc...