trying to make a button spin in Xcode with 6.3.2 objective-C - xcode6

I watched a video on how to make a button spin in Objective-C. I entered all the code exactly like in video except I got some syntex errors/ "!". I am very new to Xcode and only know some of the basics to javascript. Please help have been trying to figure this out for too long.
This is what I did in ViewController.h:
// ViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
This part in the code I get "!", it doesn't seem to like the "()". when I take that part out it's fine but the button doesn't seem to be recognized when it is called later on in ViewController.m. I'll explain what each "!" says to the write of the code where I'm getting each error. to separate it from the code ill put 2 "//" next to it.
#interface ViewController : UIViewController( // ! Method type specifier must start with '-' or '+'
I get 3 error messages on this next line of code.
IBOutlet UIButton *button; //! IBOutlet attribute ignored when parsing type, Expected selector for Objective-C method, and Expected ')'
) //! Expected identifier or '('
- (IBAction)spin:(id)sender;
#end
Now here's what I did in ViewController.m
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
- (IBAction)spin:(id)sender {
CABasicAnimation *fullRotation = [CABasicAnimation
animationWithKeyPath:#"transform.rotate" ];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 2.0;
fullRotation.repeatCount = 300;
in this part of the code I get the "!" again no matter what I do to change it
[button.layer addAnimation:fullRotation forKey:#"360"]; //! Use of undeclared identifier 'button'
}
#end

UIViewController( should not have the ( on the end.

just use:
// ViewController.h
#interface ViewController : UIViewController
#property(nonatomic, weak) UIButton *button;
- (IBAction)spin:(id)sender;
It's recommended to use #property since it generates getter/setter and you can set weak reference.
and in the implementation file add self.
[self.button.layer addAnimation:fullRotation forKey:#"360"]

Related

How to load specific url in Webview programmatically?

I am trying to make a basic search tool in cocoa. I am trying to take the string from a textfield, edit it to make a link that searches the string in google, and then feed that into the WebView. However, Xcode is not letting me use the variable I made for the Web View. I'm getting errors along the lines of "Unknown type name 'WebView'" and "Bad Receiver type '*int'". Any help would be appreciated. Here's my code for
NewWindowController.h:
#import <Cocoa/Cocoa.h>
#interface NewWindowController : NSWindowController
#property (weak) IBOutlet NSComboBox *searchselector;
- (IBAction)onclickGO:(id)sender;
#property (weak) IBOutlet WebView *webv;
#end
NewWindowController.m:
#import "NewWindowController.h"
#interface NewWindowController ()
#end
#implementation NewWindowController
#synthesize searchselector, webv;
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"Window did load");
}
- (IBAction)onclickGO:(id)sender {
NSInteger *indexofsearch = [searchselector indexOfSelectedItem];
NSLog(#"%d",indexofsearch);
//test for web view to load page
[[webv mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: #"https://google.com"]]];
}
#end
WebView is part of WebKit.framework, which is not included in the default projects.
Add #import WebKit; to the top of whatever file you're referencing the WebView object in.
Add WebKit.framework to "Link Binary with Libraries" (in Build Phases)

Expected identifier or '(' - Xcode mistake

- (IBAction)button:(id)sender {
struct label.text =
}
Whats the problem?
Why does Xcode say:
"Expected identifier or '('?"
Hopefully you have hooked up your button correctly, are you using storyboards? If you are just drag a button and a label out to the storyboard. Open the assistant editor, it's a button that looks like a tux up in the right hand corner. You need to declare what your items will do. In the assistant open your .h file. Control drag from the label to the .h and select outlet. Call it label it you wish. Then control drag from the button and select action. Call that button if you wish. This is what your .h file should look like.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)button:(id)sender;
#end
Then in your .m file you should make it look like.
import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
- (IBAction)button:(id)sender {
_label.text = #"Hello";
}
#end
I did this and when I touched the button the text changed from label to Hello. You also forgot the colon at the end.
You need to go back to your resources where you learned Objective-C and Cocoa, what you have is wrong in fundamental ways that show you don't understand the language at all
struct label.text =
This looks like just random tokens.
It looks like XCode is simply getting confused because the syntax of your statement is wrong. It looks like the line struct label.text = is trying to define a variable of type struct with the name label.text.
In Objective-C, the dot operator lets you access a method or property of an object. Trying to do this in the name of the variable doesn't make sense.

Setting label stringValue of NSTextField using Objective-C, property IBOutlet NSTextField exist in separate class

To start off with I am very new, (about 2.5 weeks) to programming in Objective-C and even newer to writing code for OS X cocoa apps. I am attempting to set the value of a NSTextField label in AppDelegate.m whose IBOutlet property exists in another class. I'm attempting to place this in the - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{} section of AppDelegate.m so that the value of the NSTextField is set before the MainMenu.xib file is loaded and displayed on screen. Here is the following code that I have so far:
AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
// Get Physical memory in MB
MemoryMonitoring *physicalMemoryObj = [[MemoryMonitoring alloc]init];
unsigned long long physicalMemoryValue = [physicalMemoryObj getPhysicalMemoryValue];
// Set the labels on the slider
RamdiskSize *sizeLabels = [[RamdiskSize alloc]init];
NSString *maxValue = [[NSString alloc] initWithFormat:#"%lluGB",(physicalMemoryValue / 1024)];
// This line is not doing what I had expected
[sizeLabels.textLabelSizeMax setStringValue:maxValue];
}
#end
MemoryMonitoring.h:
#import <Foundation/Foundation.h>
#interface MemoryMonitoring : NSObject
-(unsigned long long)getPhysicalMemoryValue;
#end
MemoryMonitoring.m:
#import "MemoryMonitoring.h"
#implementation MemoryMonitoring
-(unsigned long long)getPhysicalMemoryValue{
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
return ([pinfo physicalMemory] /1024/1024);
}
#end
RamdiskSize.h:
#import <Foundation/Foundation.h>
#interface RamdiskSize : NSObject
#property (weak) IBOutlet NSTextField *textLabelSizeMax;
#end
RamdiskSize.m:
#import "RamdiskSize.h"
#import "MemoryMonitoring.h"
#implementation RamdiskSize
#synthesize textLabelSizeMax;
#end
As commented in my AppDelegate.m, the line in question is [sizeLabels.textLabelSizeMax setStringValue:maxValue];. My only other programming experience is from VBScript and as far as I can tell Objective-C uses dot syntaxing to access properties, so this line doesn't seem to be doing what I had expected it to do. If anyone could shed some light on how this is to be done properly, I would greatly appreciate the input.
There needs to be a UIViewController or a subclass of one involved. The textField must be part of a view hierarchy rooted with a view controller's view. Maybe start with a single view application template and add the text field to that view.
Then when that view controller sees viewWillAppear fire, it can ask the MemoryMonitoring class for the value and carry on setting it's own text field.
A good sign that you're on the right track is that you'll need to add virtually nothing to your app delegate code.

Obejct-C / Xcode Not able to add/change text in UITextView

For some reason, i'm not able to change or add text in my UITextView from code.
I have made Outlet and connected (All in IB) - nothing happens.
Even tried to add -setNeedsDisplay
Do i need to set some property ? - This i driving me nuts....
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate> {
UITextView *textInfoView;
}
#property (nonatomic, retain) IBOutlet UITextView *textInfoView;
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textInfoView.text = #"Test";
}
-(void)viewDidAppear:(BOOL)animated{
textInfoView.text = #"Test";
textInfoView.text = [textInfoView.text stringByAppendingString:#"Line2"];
[textInfoView setNeedsDisplay];
It seems, really strange that this code doesn't update the text inside the UITextView. I have tried and it works for me ... I think that you don't have correctly linked the Outlet in interface builder, or you don't have associated your view controller to your view ...
UPS - Got it...
Forgot to #synthesize...
For some reason i did not get warning or "setter error" in Xcode 4.4, maybe it's a version bug ?

Problem with custom class in Objective-C

I have a little problem with the application I currenty work on. I create a simpliest project to illustrate my problem.
So, I create a "Navigate-Base Application". I add an other UITableViewController named TableViewController (the one which is created with the project is named RootViewController). I create an instance of TableViewController when I touch a line in the RootViewController.
I create a custom class named "MyCustomClass".
MyCustomClass.h (full code) :
#import <Foundation/Foundation.h>
#interface MyCustomClass : NSObject {
NSString *name;
}
#property (nonatomic, retain) NSString * name;
#end
MyCustomClass.m (full code) :
#import "MyCustomClass.h"
#implementation MyCustomClass
#dynamic name;
#end
I had a MyCustomClass attibute in TableViewController class.
TableViewController.h (full code) :
#import <UIKit/UIKit.h>
#import "MyCustomClass.h"
#interface TableViewController : UITableViewController {
MyCustomClass *aCustomObject;
}
#property (nonatomic, retain) MyCustomClass *aCustomObject;
#end
At the load of TableViewController, I try to display aCustomObject's content.
TableViewController.m (top of the file and what I modify in the template's file) :
#import "TableViewController.h"
#implementation TableViewController
#synthesize aCustomObject;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSLog(#"Name : %#",self.aCustomObject.name);
}
Before, I create and give a value to aCustomObject.name in RootViewController :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewController *detailViewController = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
detailViewController.aCustomObject.name = #"The Name";
[self.navigationController pushViewController:detailViewController animated:YES];
}
Console said :
2011-06-22 07:21:11.087
MyTestApp[12822:207] Name : (null)
I think it's a stupid thing but I don't find myself after hours of try.
Thanks a lot and excuse me for my english mistakes,
You forget to initialize your custom object in the tableViewController's viewDidLoad Method.
Try this.
- (void)viewDidLoad {
[super viewDidLoad];
if(aCustomObject == nil){
self.aCustomObject = [[[MyCustomClass alloc] init] autoRelease];
}
self.aCustomObject.name = #"";
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//this will show empty here.
NSLog(#"Name : %#",self.aCustomObject.name);
}
You use the #dynamic keyword to tell
the compiler that you will fulfill the
API contract implied by a property
either by providing method
implementations directly or at runtime
using other mechanisms such as dynamic
loading of code or dynamic method
resolution. It suppresses the warnings
that the compiler would otherwise
generate if it can’t find suitable
implementations. You should use it
only if you know that the methods will
be available at runtime.
from Apple Documentation
You are claiming in the question that you included full source for MyCustomClass.m. Where did you implement the getter and setter for the property? If you want the compiler to generate the methods for you, you should use
#synthesize name;