How do I edit an interface builder object programmatically? - cocoa

I created a label using Interface Builder, and now I want to set the background color of the label using code instead of IB's color picker. (I want to do this so that I can define the color using RGB and eventually change the colorspace for the label.)
I'm in cocoa. How do I edit the attributes of an IB object using code?
My code looks like this:
//.h file
#import <UIKit/UIKit.h>
#interface IBAppDelegate : NSObject {
UILabel *label;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#end
//.m file
#import "IBAppDelegate.h"
#implementation IBAppDelegate
#synthesize label;
(memory stuff...)
#end

- (void)applicationDidBecomeActive:(UIApplication*)application
{
self.label.backgroundColor = [UIColor colorWithRed:0.1f
green:0.2f
blue:0.3f
alpha:1.0f];
}

label.backgroundColor = [UIColor greenColor]
There's nothing special about the objects in your XIB file. They're just normal objects.

Related

IB_DESIGNABLE Class not rendering on Interface Builder

This is my first try with IB_DESIGNABLE on Xcode.
I have this class to add color to a NSView.
header
#import <Cocoa/Cocoa.h>
IB_DESIGNABLE #interface NSViewComCor : NSView
#property (nonatomic, weak) IBInspectable NSColor *backgroundColor;
#end
implementation
#import "NSViewComCor.h"
#implementation NSViewComCor
#synthesize backgroundColor = _backgroundColor;
- (void)awakeFromNib {
[super awakeFromNib];
[self setWantsLayer:YES];
self.backgroundColor = [NSColor whiteColor]; //default color
}
- (NSColor *) backgroundColor
{
CGColorRef colorRef = self.layer.backgroundColor;
return [NSColor colorWithCGColor:colorRef];
}
- (void) setBackgroundColor:(NSColor *)backgroundColor
{ // color should change when changed on interface builder inspectable color box
self.layer.backgroundColor = backgroundColor.CGColor;
_backgroundColor = backgroundColor;
}
even with IB_DESIGNABLE, this class does not render with the correct color on interface builder... why?
As the point out in WWDC 2014 "What's New in Interface Builder" video, you have to:
Create framework.
Create class.
Mark view as designable (and properties as inspectable).
Go back to the main project and specify the base class in Interface Builder.
For example, I added a new "framework" target to the project, and added the following NSView subclass source to that framework:
IB_DESIGNABLE #interface CustomView : NSView
#property (nonatomic, strong) IBInspectable NSColor *backgroundColor;
#end
and
#implementation CustomView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[self.backgroundColor setFill];
NSRectFill(dirtyRect);
}
#end
Then, when I went back to my main project and tried adding this CustomView as a subview on my storyboard, the "background color" was IB "inspectable" and I could see the changes in the color immediately rendered in IB.

Undeclared Identifier after the void

I have got this error on the line where the void is it says undeclared identifier for sliderDidChange can some one please help me with this i can;t find any answers.
h. file
//
// LightViewController.h
// Flash Light
//
// Created by John Whitney on 12-07-27.
// Copyright (c) 2012 Perfect Programs. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface LightViewController : UIViewController {
IBOutlet UISlider *slider;
IBOutlet UISlider *theslider;
IBOutlet UISlider *customslider;
UIButton *onButton;
UIButton *offButton;
UIImageView *onView;
UIImageView *offView;
}
#property(nonatomic, strong) IBOutlet UIButton *onButton;
#property(nonatomic, strong) IBOutlet UIButton *offButton;
#property(nonatomic, strong) IBOutlet UIImageView *onView;
#property(nonatomic, strong) IBOutlet UIImageView *offView;
#property(nonatomic, readonly) float torchLevel;
-(void)sliderDidChange:(UISlider *)slider;
-(BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError;
-(BOOL)supportsAVCaptureSessionPreset:(NSString *)preset;
-(IBAction)torchOn:(id)sender;
-(IBAction)torchOff:(id)sender;
#end
m.file
UISlider *localslider = [[UISlider alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 40.0f)];
localslider.maximumValue = 1.0f;
localslider.minimumValue = 0.0f;
[localslider setContinuous:YES];
[localslider addTarget:self action:#selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:localslider];
-(void)sliderDidChange:(UISlider *)slider
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchModeOnWithLevel:slider.value error:NULL];
[device unlockForConfiguration];
}
can someone please help quick
Well the problem may be you added an { anywhere above that line of code
Please make sure all open braces are closed properly
I assume 1) you have synthesized your properties in the .m file, 2) you have implemented all methods declared in the .h file also there, and 3) the 6 statements in your .m file with which you create your localslider object, i.e. those followed by -(void)sliderDidChange:(UISlider *)slider, are part of the viewDidLoad method.
In this case, you should only get a warning that the argument slider of your -(void)sliderDidChange:(UISlider *)slider method hides your instance variable with the same name in this method.

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

Xcode Storyboard - Transfer of data

I'm new to storyboarding so the answer to this may be simplistic,
I've created a viewController in which a UITextField is present. My test is to transfer the data (text) from that text field into a viewController that is pushed onto the screen.
The coding that i have is as follows:
ViewController1.h -
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : UIViewController
#property (nonatomic, retain) IBOutlet UITextField *inputText;
#end
ViewController1.m
#import "ViewController1.h"
#implementation ViewController
#synthesize inputText;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showTextController"]) {
ViewController2 *vc2 = [segue destinationViewController];
vc2.selectedText.text = self.inputText.text;
}
}
#end
ViewController2.h -
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *selectedText;
#end
ViewController2.m
#import "ViewController2.h"
#implementation ViewController2
#synthesize selectedText;
#end
The segue between viewController1 and 2 in storyboard is referred to as 'showTextController'.
Is this the correct coding for something so simple? Do i need to be using 'ViewDidLoad' method along with the prepareForSegue:sender method?
It looks correct to me. In fact, this is simpler than we have had in the past since the storyboard takes care instantiating our view controller objects. One thing to note is that if you're using ARC then you shouldn't be retaining your UILabel.
Hope this helps.

Change WebView position Cocoa

I'm quite a newbie to Cocoa & Mac programming. I have a WebView and I want to move it. Is there a way to change the coordinates of the WebView?
EDIT: Some relevant code:
NistractAppDelegate.m
#import "NistractAppDelegate.h"
#implementation NistractAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[window setAlphaValue:0];
[window setBackgroundColor:[NSColor colorWithCalibratedRed:0.125490196078431 green:0.125490196078431 blue:0.125490196078431 alpha:1]];
[mainWebView setBackgroundColor:[NSColor colorWithCalibratedRed:0.125490196078431 green:0.125490196078431 blue:0.125490196078431 alpha:1]];
[[mainWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
mainWebView.frame = NSMakeRect(mainWebView.frame.origin.x + 10, mainWebView.frame.origin.y, mainWebView.frame.size.width, mainWebView.frame.size.height);
[window makeKeyAndOrderFront:self];
[[window animator] setAlphaValue:1];
}
#end
NistractAppDelegate.h
#import <Cocoa/Cocoa.h>
#interface NistractAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSView *mainView;
IBOutlet id mainWebView;
}
#property (assign) IBOutlet NSWindow *window;
#end
If you just want to move the entire webview you need set it's frame.
WebView.frame = NSMakeRect(x,y,width,height);
Eg if you want to just shift it left by ten pixels:
WebView.frame = NSMakeRect(WebView.frame.origin.x + 10, WebView.frame.origin.y, WebView.frame.size.width, WebView.frame.size.height);
When in doubt always you look in the documentation, which you can do from within side XCode, by selecting some text and right clicking then selecting Find Text In Documentation
Or use Google by searching "{Class name} class reference" Eg "nsview class reference"

Resources