How do you make a UIImageView's image change when a button is pushed? The UIImageView and UIButton are on two different views - uiimageview

I have a UIButton and a UIImageView. The UIButton and UIImageView are on different views in storyboard. What I want to happen is when the button is pressed, it switches views and the UIImageView switches to a different image. Right now, the view changes, but the UIImageView doesn't change.
Here is the ViewController.h file
#import <UIKit/UIKit.h>
#import "Player.h"
#import "Space.h"
#interface ViewController : UIViewController
{
IBOutlet UIImageView* space0_0;
Player* m_player;
Space* m_spaces[16][16];
}
-(IBAction)startGame;
#end
The reason there is an array of spaces is that I want there to eventually be a 16x16 board of UIImageViews. Right now I only have 1 image view just to get the first one working before moving on
Here is the IBAction connected to the button that is supposed to change the UIImageView
-(IBAction)startGame
{
[m_spaces[0][0] setImageView:space0_0];
[m_spaces[0][0] displaySpace];
}
I made a class called Space. Here is the Space.h file
#import <Foundation/Foundation.h>
#interface Space : NSObject
{
UIImageView* m_imageView;
}
-(void)setImageView:(UIImageView*)imageView;
-(UIImageView*)imageView;
-(void)displaySpace;
#end
Here is the Space.m file
#import "Space.h"
#implementation Space
-(void)setImageView:(UIImageView*)imageView
{
m_imageView = imageView;
}
-(UIImageView*)imageView
{
return m_imageView;
}
-(void)displaySpace
{
m_imageView.image = [UIImage imageNamed:#"player.png"];
}
#end
Can anyone tell me where I'm going wrong here? The only problem is that the UIImageView's image does not change.
Edit: when I change the startGame method to:
-(IBAction)startGame;
{
space0_0.image = [UIImage imageNamed:#"player.png"];
}
So the problem is somewhere in my Space class.

Related

Sse IBInspectable and IB_DESIGNABLE Bug changes not show in storyboard?

I reading this article IBInspectable / IBDesignable and flow step,But I found change not immediately rendered on storyboard.Can anyone known why?
My Xcode Version:7.1
My main code:
ViewController.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
#interface ViewController : UIViewController
#end
UIView+CornerRadius.h
#import <UIKit/UIKit.h>
#interface UIView (CornerRadius)
#property(nonatomic ,assign) IBInspectable CGFloat cornerRadius;
#end
UIView+CornerRadius.m
#import "UIView+CornerRadius.h"
#implementation UIView (CornerRadius)
-(void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = cornerRadius > 0;
}
-(CGFloat)cornerRadius {
return self.layer.cornerRadius;
}
#end
I've tested your code in Xcode, and I found that if you declare the view on storyboard as an instance of UIView+CornerRadius, it really don't work. But if you create a subclass of UIView or your UIView+CornerRadius category, and declare the view as this subclass, it will work fine.
I'll attach a screenshot to demonstrate:

Change background image of a button programmatically cocoa obj-c

I am creating NSCollection View (in Cocoa) with many identical buttons in size and appearance, except for their icons (or you could probably call them Background Images).
In my xib file I have the button binded to the Collection View (Model key path: representedObject.foto) and the Array Controller has class of MyButton and Keys: foto.
I created My Button class with NSImage property - is this the right way to do it?
How do I set an Image that I have in my project in AppController.m so that it appears on the button when running my app?
Everything else should be fine, because I have previously create NSCollectionView with Labels and it worked.
MyButton.h
#import <Cocoa/Cocoa.h>
#interface MyButton : NSButton
#property(retain, readwrite) NSImage *foto;
#end
MyButton.m
#import "MyButton.h"
#implementation MyButton
#end
AppController.h
#interface AppController : NSObject {IBOutlet NSArrayController *controller;}
#property (strong) NSMutableArray *modelArray;
#end
AppController.m
#import "AppController.h"
#import "MyButton.h"
#implementation AppController
- (void) awakeFromNib {
MyButton *FirstOne = [[MyButton alloc] init];
//FirstOne.foto = ???
_modelArray = [[NSMutableArray alloc] init];
[controller addObject:FirstOne];}
#end
Where is your image located in your project? If its in an .xcasset file, then you can set the image with:
FirstOne.image = [NSImage imageNamed:#"StatusBarIcon"];
You can learn about storing images in assets here. I would recommend using these assets as it allows you to keep all the images that you may use in your project organised, and it helps reduce the hassle of maintaining images for multiple resolutions.

switching views with NSMenuItem IBAction does not work

In a document based Cocoa App I want to fill a NSBox in the Document.xib with a view,
by selecting the view with a NSMenuItem. However, the box is not updated with the view.
If I insert a button in the Document.xib, which is connected with the same IBAction as the NSMenuItem, the app works in the expected way.
I created the tree files:
- ViewController.h
- ViewController.m
- prettyView.xib
In ViewController.m the XIB File of the view is initialized.
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (id)init
{
if(![super initWithNibName:#"prettyView" bundle:nil]){
return nil;
}
[self setTitle:#"Pretty View"];
return self;
}
#end
The Document.h contains outlets for the box and two buttons.
One button fills the box with the view, the other one clears the box.
// Document.h
#import <Cocoa/Cocoa.h>
#class ViewController;
#interface Document : NSDocument
#property (weak) IBOutlet NSBox *contentBox;
- (IBAction)fillBox:(id)sender;
- (IBAction)clearBox:(id)sender;
#property ViewController * myViewController;
#end
In Document.m the view controller is instantiated.
// Document.m
#import "Document.h"
#import "ViewController.h"
#interface Document ()
#end
#implementation Document
- (instancetype)init {
self = [super init];
if (self) {
_myViewController = [ViewController new];
}
return self;
}
The methods for the IBActions are implemented in Document.m too.
- (IBAction)fillBox:(id)sender {
NSLog(#"Fill Box selected from %#", [sender className]);
[self.contentBox setContentView:[self.myViewController view]];
}
- (IBAction)clearBox:(id)sender {
NSLog(#"Clear Box selected");
[self.contentBox setContentView:nil];
}
The method fillBox is connected to one of the both buttons as well as to the NSMenuItem.
Pressing the button, a message is written to the console and the view is shown in the box.
Selecting the NSMenuItem, a message is written too, but the view is not displayed in the box.
The IBActions must not be connected with Document but with First Responder of MainMenu.xib.

Xcode Random Image Button, and email button

I'm trying to create an app that creates a random image button, and the code works. when my app generates an image, I'd like to create a button from which i can send an email with the screenshot attached...i know the codes for both actions, but when i put them in the controller h and m I've many errors...please help me!
#interface __3_ProductionsViewController : UIViewController
<MFMailComposeViewControllerDelegate>
-(IBAction)openMail:(id)sender;
-(IBAction)randomimagebutton;
{
IBOutlet UIImageView *imageview;
}
#end
Depending on the errors you are getting you might want to rearrange your formatting a little. This is how I would declare the h and m.
// __3_ProductionsViewController.h
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface __3_ProductionsViewController : UIViewController
<MFMailComposeViewControllerDelegate>
#property (nonatomic, retain) IBOutlet UIImageView *imageview;
-(IBAction)openMail:(id)sender;
-(IBAction)randomimagebutton:(id)sender;
#end
// __3_ProductionsViewController.m
#import "__3_ProductionsViewController.h"
#implementation UpdateViewController
#synthesize imageView;
-(IBAction)openMail:(id)sender {
}
-(IBAction)randomimagebutton:(id)sender {
}
#end
You'll need to connect the IBActions using interface builder as well as drag a connection to your imageView

Cocoa - loading a view from a nib and displaying it in a NSView container , as a subview

I've asked about this earlier but the question itself and all the information in it might have been a little confusing, plus the result i want to get is a little more complicated. So i started a new clean test project to handle just the part that im interested to understand for the moment.
So what i want, is basically this: i have a view container (inherits NSView). Inside, i want to place some images, but not just simple NSImage or NSImageView, but some custom view (inherits NSView also), which itself contains a textfield and an NSImageView. This 'image holder' as i called it, is in a separate nib file (im using this approach since i am guiding myself after an Apple SAmple Application, COCOA SLIDES).
The results i got so far, is something but not what i am expecting. Im thinking i must be doing something wrong in the Interface Builder (not connecting the proper thingies), but i hope someone with more expertise will be able to enlighten me.
Below i'll try to put all the code that i have so far:
//ImagesContainer.h
#import <Cocoa/Cocoa.h>
#interface ImagesContainer : NSView {
}
#end
//ImagesContainer.m
#import "ImagesContainer.h"
#import "ImageHolderView.h"
#import "ImageHolderNode.h"
#class ImageHolderView;
#class ImageHolderNode;
#implementation ImagesContainer
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
//create some subviews
for(int i=0;i<3;i++){
ImageHolderNode *node = [[ImageHolderNode alloc] init];
[self addSubview:[node rootView]];
}
}
NSRunAlertPanel(#"subviews", [NSString stringWithFormat:#"%d",[[self subviews] count]], #"OK", NULL, NULL);
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
[[NSColor blackColor] set];
NSRectFill(NSMakeRect(0,0,dirtyRect.size.width,dirtyRect.size.height));
int i=1;
for(NSView *subview in [self subviews]){
[subview setFrameOrigin:NSMakePoint(10*i, 10)];
i++;
}
}
#end
//ImageHolderView.h
#import <Cocoa/Cocoa.h>
#interface ImageHolderView : NSView {
IBOutlet NSImageView *imageView;
}
#end
//ImageHolderVIew.m
#import "ImageHolderView.h"
#implementation ImageHolderView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
[[NSColor blueColor]set];
NSRectFill(NSMakeRect(10,10, 100, 100));
//[super drawRect:dirtyRect];
}
#end
//ImageHolderNode.h
#import <Cocoa/Cocoa.h>
#class ImageHolderView;
#interface ImageHolderNode : NSObject {
IBOutlet ImageHolderView *rootView;
IBOutlet NSImageView *imageView;
}
-(NSView *)rootView;
-(void)loadUIFromNib;
#end
//ImageHolderNode.m
#import "ImageHolderNode.h"
#implementation ImageHolderNode
-(void)loadUIFromNib {
[NSBundle loadNibNamed:#"ImageHolder" owner: self];
}
-(NSView *)rootView {
if( rootView == nil) {
NSRunAlertPanel(#"Loading nib", #"...", #"OK", NULL, NULL);
[ self loadUIFromNib];
}
return rootView;
}
#end
My nib files are:
MainMenu.xib
ImageHolder.xib
MainMenu is the xib that is generated when i started the new project.
ImageHolder looks something like this:
image link
I'll try to mention the connections so far in the xib ImageHolder :
File's Owner - has class of ImageHolderNode
The main view of the ImageHolder.xib , has the class ImageHolderView
So to resume, the results im getting are 3 blue rectangles in the view container, but i cant seem to make it display the view loaded from the ImageHolder.xib
If anyone wants to have a look at the CocoaSlides sample application , its on apple developer page ( im not allowed unfortunately to post more than 1 links :) )
Not an answer, exactly, as it is unclear what you are asking..
You make a view (class 'ImagesContainer'). Lets call it imagesContainerView.
ImagesContainerView makes 3 Objects (class 'ImageHolderNode'). ImagesContainerView asks each imageHolderNode for it's -rootView (maybe 'ImageHolderView') and adds the return value to it's view-heirarchy.
ImagesContainerView throws away (but leaks) each imageHolderNode.
So the view heirachy looks like:-
+ imagesContainerView
+ imageHolderView1 or maybe nil
+ imageHolderView2 or maybe nil
+ imageHolderView3 or maybe nil
Is this what you are expecting?
So where do you call -(void)loadUIFromNib and wait for the nib to load?
In some code you are not showing?
In general, progress a step at a time, get each step working.
NSAssert is your friend. Try it instead of mis-using alert panels and logging for debugging purposes. ie.
ImageHolderNode *node = [[[ImageHolderNode alloc] init] autorelease];
NSAssert([node rootView], #"Eek! RootView is nil.");
[self addSubview:[node rootView]];
A view of course, should draw something. TextViews draw text and ImageViews draw images. You should subclass NSView if you need to draw something other than text, images, tables, etc. that Cocoa provides.
You should arrange your views as your app requires in the nib or using a viewController or a windowController if you need to assemble views from multiple nibs. Thats what they are for.
EDIT
Interface Builder Connections
If RootView isn't nil then it seems like you have hooked up your connections correctly, but you say you are unclear so..
Make sure the IB window is set to List view so you can see the contents of you nib clearly.
'File's Owner' represents the object that is going to load the nib, right? In your case ImageHolderNode.
Control Click on File's owner and amongst other things you can see it's outlets. Control drag (in the list view) from an outlet to the object you want to be set as the instance var when the nib is loaded by ImageHolderNode. I know you know this already, but there is nothing else to it.
Doh
What exactly are you expecting to see ? An empty imageView? Well, that will look like nothing. An empty textfield? That too, will look like nothing. Hook up an outlet to your textfield and imageView and set some content on them.

Resources