UIImageView Implementation - objective-c

I'm just wondering about this extremely simple thing. How do you implement one, ONE, static image to your storyboard?
What i have tried so far is to have ViewController.h to
#property (strong, nonatomic) IBOutlet UIImageView *image;
and viewController.m to
_imageArray = #[#"hemla.jpg"]; //Logo for some stuff, added in resources.
image.image = [UIImage imageNamed:_imageArray[0]];
Where I either put "0" at the end, which results in nothing at all (But app is running), or "1" for a "Thread 1: Signal SIGABRT" error. (After compiling, when launching app in Simulator)
Is there a better way to implement images, or is it just me doing something wrong?

If you're using a storyboard, you don't need code at all. Just setup the imageView in the interface builder. You can set the image name there.
If you want to do it in the code, your code is right, but probably at the wrong place, e.g. the init method. The imageView has not been created at this point. You should use viewDidLoad: or any other point after this.

Related

Changing image in different view

I am attempting to change the character image in the game that I am creating but I am having some issues. I have a view controller where I will have all of the characters as their own buttons, and by selecting a button it should change the character image in the GameViewController
change.h
- (IBAction)charOne:(id)sender;
change.m
- (IBAction)charOne:(id)sender
{
GameViewController *changeView = [GameViewController game];
UIImageView *Romo = [GameView changeView];
[ChangeView setImage:[UIImage imageNamed:#"testOne.png"]];
NSLog(#"Test");
}
GameViewController.m
{
IBOutlet UIImageView *changeView;
}
#property (strong, nonatomic) IBOutlet UIImageView *changeView;
This code isn't working for me, I do not receive an error message but nothing is changed after the image is selected.
On the GameViewController the UIImage name that I am attempting to change the image for is named Romo.
Any help will be appreciated
try :
[Romo setImage:[UIImage imageNamed:#"testOne.png"]];
instead of :
[ChangeView setImage:[UIImage imageNamed:#"testOne.png"]];
You're calling the setImage method on a class rather than an instance of a class. Since Romo is the instance of UIImageView you want to affect (which is a reference pointer to the UIImageView on your game view controller), that should be the instance you affect.
Also, bad practice to name instances starting with a capital letter -- they turn out looking like classes.

UILabel setting x,y coordintes not working

I'm new to x-code and I am trying to set the x,y position of a UILabel but I can't figure out why it is not working.
.h
#interface ViewController:UIViewController{
IBOutlet UILabel *badgeslabel;
}
#property (nonatomic,retain)IBOutlet UILabel *badgeslabel;
#end
.m
#implementation ViewController
#synthesize badgeslabel;
-(void)setBadge{
[badgeslabel setAlpha:.5];
[badgeslabel setCenter:CGPointMake(160,30)];
}
The setAlpha works, but the setCenter don't. Also, when I put the code in an IBAction the setCenter works but I don't know why.
I'm on xcode 5.0
Any help is appreciated, thank you.
As stated by Joel, if you have autolayout enabled then XCode won't allow you to manually set the x & y values of UIViews within a view controller because they are using constraints, attempting to do this will most likely break any constraints you have setup or XCode has added automatically and distort the position of any other UIViews you have in your View Controller.
If you don't want to use autolayout then simply disable it by going to the File inspector in interface builder (click your storyboard file), and untick "Use Auto Layout". This will revert to the old "Spring & Struts" way of laying out and allow you to set the position of your UIViews in code.

Obj-c UIImageView is null when calling from outside class

I have a UIViewController containing a UIImageView that has been correctly wired up (I can set image inside controller).
I have also set an iVar using
UIImageView *_imgQRCode;
and a property using
#property (strong, nonatomic) IBOutlet UIImageView *imgQRCode;
Synthesized using:
#synthesize imgQRCode = _imgQRCode;
But when I call something like below I nothing happens the image and when I inspect in Xcode the reference is memory reference is 0x00000000. note the code works when called inside the ViewController. The following is called from any other view controller with controller instantiated as follow:
QRPopupViewController *qrPop = [[QRPopupViewController alloc] initWithNibName:#"QRPopupViewController" bundle:nil];
[controller.imgView setImage:[UIImage imageNamed:#"sample.png"]];
Any ideas how to fix this and why i'm getting null?
Firsty: outlets should be weak
#property (weak, nonatomic) IBOutlet UIImageView *imgQRCode;
You have to hook imgQRCode outlet to your viewController, and you can use it inside viewDidLoad.
You will get null for all UI Control's instances which are not part of the currently visible view controller. So due to this all your calls to that UI control will not perform the UI related operation.
If you want to update any UI control which is not part of currently visible view controller, you can use app delegate. It is the only place which is centralized in between all view controllers.
There is a mechnism, called "protocol delegate" to call some method to update UI part from the currently visible view controller. You can go through this URL for detail about this.
If you are selecting the approach of "protocol delegate", then explore the method execution flow in detail. When you call a protocol from a method of currently visible view controller, the corresponding protocol implementation method will be called after the completion of the caller method. So you need to take care of synchronization.

Getting objects like button from view. iOS

I created a button in my cover.h file for my first view
#import <UIKit/UIKit.h>
#interface cover : UIViewController
{
IBOutlet UIButton *Enter;
}
#property (nonatomic, retain) UIButton *Enter;
-(IBAction)buttonpressed:(id)sender;
#end
and connected it with the actual button in the inteface builder by choosing File's Owner in the tiny box that gives you the choice of File's Owner, First Responder, and View
Then I went to cover.m file and added the following code
-(IBAction)buttonpressed:(id)sender
{
[[NSBundle mainBundle] loadNibNamed:#"nextView" owner:self options:nil];
NSLog(#"pressed");
}
SO when I go to the nextView.xib and modify nextView.m and nextView.h and access its buttons and do the same thing I did for cover.xib cover.m and cover.h , it doesn't work properly.
What happens is that when I click the enter button in the cover view it shuts down the app. This does not happen until I connected the button to function and outlet (Meaning when it was just switching views and the second view wasn't doing anything it would work)
Thank you for any help you can give. Sorry if I haven't given enough information, kinda new at this, but as find out more info I should have had, I will add it.
Thank you
Edit 1 :
I did not notice anywhere where it said there was an error or anything like that. It built correctly
It's convention to capitalize class names. I'd Suggest CoverViewController. This makes it more obvious, when reading your code, what we're looking at.
What is your intent here? To show nib after nib of content?
Is the "files owner" of every nib a CoverViewController?
What you have actually done is (catastrophically) reloaded the views for the existing controller. This will not end well.
What you want to do is create another instance of the same class:
-(IBAction)buttonpressed:(id)sender {
CoverViewController nextController = [[CoverViewController alloc] initWithNibName:#"nextView"];
[navigationController pushViewController:nextController animated:NO];
}
If you're not using a nav controller, you probably want to be.
Read up on the View Controller Programming Guide.

Unable to set text in NSTextView

::Edit::
It works now? haha, maybe I just didn't save my latest IB...who the hell knows.
Thanks!
Hey,
This is my first time playing around with desktop applications (I have experience with iphone applications) and I'm stuck - My text view will not update to show any text:
.m:
#synthesize textView;
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
NSLog(#"txt = %#", txt);
[textView setString:txt];
.h:
IBOutlet NSTextView *textView;
}
#property(nonatomic, retain) IBOutlet NSTextView *textView;
And IB says textView -> Text View, so everything looks good:
NSLog above outputs the contents of the url resource Im fetching
So, what am I missing?
Double check if the outlets are done in IB. That's probably the reason (textView is probably nil here).
Where is this setString code being called? It's possible the IB outlet isn't instantiated yet. You can check this with a simple
if (textView) {
NSLog(#"textView is not nil);
} else {
NSLog(#"textView is nil");
}
To be sure that everything is set up when you call this, make sure it's after 'awakeFromNib' is called in any objects created through IB.
See NSNibAwaking Protocal
Can you double check NSTextView properties like isEditable, textColor etc to make sure none of them are set incorrectly.
Other than that the code looks good.
I reconnected everything in IB, saved, rebuilt and it worked...= [