Moving an UIView - newbie iphone sdk - iphone

Hey guys... today is my first day hacking away on the iphone SDK. Having a blast but have a quick question.
I'm trying to move an UIView around the screen dynamically by sending it information from a slider. My slider is working fine but I cant seem to figure out how to get the UIView to move.
My .h file...
#interface Slider_BallViewController : UIViewController
{
IBOutlet UISlider *theslider;
IBOutlet UITextField *ytext;
IBOutlet UIView *theball;
}
- (IBAction)moveVert:(id)sender;
My .m file...
- (IBAction)moveVert:(id)sender
{
int progressAsInt = (int)(theslider.value);
NSString *newText = [[NSString alloc] initWithFormat:#"%d", progressAsInt];
ytext.text = newText;
theball.frame.origin.y += progressAsInt;
}
I get an error on the frame.origin line in my .m file that says... lvalue required as left operand assignment. Not sure what im doing wrong.
Any help is great, thanks.

If you want to modify a UIView's frame property, you should do it by following:
CGRect curFrame = theball.frame;
curFrame.origin.y += progressAsInt;
theball.frame = curFrame;

Related

Linking stepper to Slider UIObject in C4

I am very new to C4 so please be gentle...
If I want to link a slider value to a label this is done with NSString stringWithFormat... e.g.:
self.mylabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
I added a stepper object as well, and now it also updates mylabel:
self.mylabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
But it would be intuitive if the slider position follows the label value when I'm using the stepper. but .value is not an available property in UILabel... so how do I take the mylabel.text property and push that into the slider.value property without getting a datatype mismatch error?
This question has 2 answers, how to do it using C4 objects and how to do it with Interface Builder / UIControls. I'll show both ways, UI first so that I can compare the C4 way afterwards.
UIControl
To do this with UIControls first set up your C4WorkSpace.h header so that it has the following methods and properties:
#property (assign, nonatomic) IBOutlet UILabel *myLabel;
#property (assign, nonatomic) IBOutlet UISlider *mySlider;
#property (assign, nonatomic) IBOutlet UIStepper *myStepper;
-(IBAction)sliderWasUpdated:(UISlider *)slider;
-(IBAction)stepperWasUpdated:(UIStepper *)stepper;
Then, in your drag all three components onto your projects XIB file (i.e. a UISlider, UILabel and UIStepper). Link the action sliderWasUpdated: to the slider using the valueChanged option, and the stepperWasUpdated: action to the stepper also using the valueChanged option. You do this step by selecting C4Canvas.xib from your project then right-clicking on the yellow cube, then dragging from the actions listed in the pop-up menu to each of the objects that you recently placed on the canvas.
Next, add the following code to your C4WorkSpace.m file:
#implementation C4WorkSpace
-(void)setup {
self.myStepper.minimumValue = 0.0f;
self.myStepper.maximumValue = 10.0f;
self.mySlider.minimumValue = 0.0f;
self.mySlider.maximumValue = 10.0f;
}
-(IBAction)sliderWasUpdated:(UISlider *)slider {
slider.value = [C4Math round:slider.value];
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
self.myStepper.value = slider.value;
[self.myLabel sizeToFit];
}
-(IBAction)stepperWasUpdated:(UIStepper *)stepper {
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
self.mySlider.value = stepper.value;
[self.myLabel sizeToFit];
}
#end
In the setup we make sure that the min/max values of both UI objects are the same (so that we can keep them matched up).
In the stepperWasChanged: method we do two things:
We use the stepper's value to set the label's text
We also use the stepper's value to set the slider's value!
In the sliderWasChanged: method we do the same thing, updating the stepper, but we also round the value of the slider so that it increments in steps (just to keep things tidy).
C4Control
To do the same with C4 objects, instead of native UI objects, we set things up a little differently. First, we don't add anything to our C4Canvas.xib, instead we'll set the objects up manually.
In your C4WorkSpace.h file, add the following lines of code:
#property (readwrite, nonatomic, strong) C4Label *myLabel;
#property (readwrite, nonatomic, strong) C4Slider *mySlider;
#property (readwrite, nonatomic, strong) C4Stepper *myStepper;
-(void)sliderWasUpdated:(C4Slider *)slider;
-(void)stepperWasUpdated:(C4Stepper *)stepper;
Notice that most of this is the same except we're using C4 instead of UI prefixes. Also, we call our methods -(void) instead of -(IBAction) because we're not using Interface Builder.
Next, add the following code to your C4WorkSpace.m:
#implementation C4WorkSpace
-(void)setup {
[self createAddObjects];
//calibrate the min/max values
self.myStepper.minimumValue = 0.0f;
self.myStepper.maximumValue = 10.0f;
self.mySlider.minimumValue = 0.0f;
self.mySlider.maximumValue = 10.0f;
}
-(void)sliderWasUpdated:(C4Slider *)slider {
slider.value = [C4Math round:slider.value];
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
self.myStepper.value = slider.value;
[self.myLabel sizeToFit];
}
-(void)stepperWasUpdated:(C4Stepper *)stepper {
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
self.mySlider.value = stepper.value;
[self.myLabel sizeToFit];
}
-(void)createAddObjects {
//set up the objects
self.myLabel = [C4Label labelWithText:#"values"];
self.myStepper = [C4Stepper stepper];
self.mySlider = [C4Slider slider:CGRectMake(0, 0, 192, 44)];
//position them
CGPoint centerPoint = CGPointMake(self.canvas.center.x,
self.canvas.center.y - 100);
self.myStepper.center = centerPoint;
centerPoint.y += 100;
self.myLabel.center = self.canvas.center;
centerPoint.y += 100;
self.mySlider.center = centerPoint;
//set up action bindings
[self.mySlider runMethod:#"sliderWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.myStepper runMethod:#"stepperWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.canvas addObjects:#[self.myStepper,self.myLabel,self.mySlider]];
}
#end
DIFFERENCES
The major difference between the two approaches is whether or not you use Interface Builder. In the C4 approach we need to add a method called createAddObjects to our project so that our slider, label and stepper all get added to the canvas.
This method also contains the methods for binding the actions of our C4UIElements to our code, which happens in the lines:
[self.mySlider runMethod:#"sliderWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.myStepper runMethod:#"stepperWasUpdated:"
target:self
forEvent:VALUECHANGED];
Once these are set up the only difference is specifying the use of C4 objects instead of UI objects, like:
-(void)sliderWasUpdated:(C4Slider *)slider {...}
instead of
-(IBAction)sliderWasUpdated:(UISlider *)slider {...}

iphone - Can't format label

I can't format a label, here's my code:
ct.text = [NSString stringWithFormat:#"%i:%i", currentTimeMin, currentTime];
so ct is the label and it won't display the value.
ct in the .h file: #property (nonatomic, retain) IBOutlet UILabel *ct;
I used (strong, nonatomic) as well but that didn't change anything
currentTimeMin is set to 0 and currentTime to self.audioPlayer.currentTime I think the problem could be there.
I have also tried to put ct.text = [NSString stringWithFormat:#"hey"]; didn't work either
Please help me! I am getting so sick of this. Thanks!
**EDIT**
And a weird thing is that I set up a label in another ViewController and then it worked but it's just in that single view that it won't work. Somehow, someway.
Judging from your comments and tests...
Either
(1) You are not referring to the label correctly. It should be self.ct
Or
(2) You have not wired up the label correctly. Please check it again. If you mean to assign content to a label.text but have not connected it to the property you will get a null result (as far as the program is concerned you have not alloc/init'ed the property).

Unrecognized selector sent to instance when extending UIImageView

I've searched through all similar questions but I couldn't find the answer. I'm completely stumped on this... I have a class that extends UIImageView. So here's my .h file:
#interface Paper : UIImageView {
#public
CGFloat drag;
}
#property (nonatomic, assign) CGFloat drag;
#end
I'm synthesizing drag properly in my implementation.
Now, in my ViewController, I create and initialize a Paper object like so:
NSString *tempPath = [[NSBundle mainBundle] pathForResource:#"picture" ofType:#"png"];
UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:tempPath];
Paper *tempPaper = [[UIImageView alloc] initWithImage: tempImage];
The object is created right, I can later display it as a subview and all that jazz, but when I try to change my custom property immediately after the above using a line like:
tempPaper.drag = 1.0;
or
[tempPaper setDrag:1.0];
I get [UIImageView setDrag:]: unrecognized selector sent to instance as an error. I have tried every different method I've found but it won't allow me to set the value of my custom properties (the non-UIImageView ones) for the Paper class. What is it I'm missing? I've spent 2 hours on this and it's driving me nuts, I don't see what's wrong.
I've also tried initializing drag in Paper's initWithImage method, but it doesn't work either.
Paper *tempPaper = [[Paper alloc] initWithImage: tempImage];
Create object of Paper class instaed of UIImageView.
Paper *tempPaper = [[Paper alloc] initWithImage: tempImage];

CALayer does not seem to be defined

I wanted to add rounded Rects to a UITextView as explained in How to style UITextview to like Rounded Rect text field?. However when I do this I receive an error message that the methods used there are not found. I think this is weird as they are also still in the documentation. On the other side XCode does not show anything in autocomplete on the field layer. Is this field somehow deprecated or where could be the problem?
Here the two code snippets I was using:
#interface AddItemViewController : UIViewController {
IBOutlet UITextView *detailsTextView;
}
#end
Hier will ich dann die Eigenschaften aendern.
- (void)viewDidLoad
{
[super viewDidLoad];
// add rounded rects to detailsTextView
//first leads to warning that method is unknown
[detailsTextView.layer setCornerRadius:8.0f];
// displays error that property is not found on object of type CALayer *
textView.layer.cornerRadius = 8.0f;
detailsTextView.clipsToBounds = YES;
}
Add the QuartzCore framework in your project
Include this in your .h file
#import <QuartzCore/QuartzCore.h>

UITextField isn't working

am new to objective-c programming, so sorry if it's a silly question but i looked thoroughly and couldn't find the error,
p.s: english isn't my first language so programming keywords might be unintentionally used
I've set up a txtfield and defined some of it's properties, such as the keyboard-number pad and stuff, the problem is, when i run it on the simulator, nothing happens, the defult keyboard is called.
i'm just trying to do simple stuff to get it in my head,
in my .h, i have
#interface practiceViewController : UIViewController <UITextFieldDelegate>
{
UITextField *userInput;
}
#property (nonatomic, retain, readwrite) UITextField *userInput;
#end
and in my .m
-(UITextField *)userInput{
if (userInput == nil) {
userInput=[[UITextField alloc]init];
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
}
return userInput;
}
i also tried to make it as an input, but for some reson it's not working
int nInput=[[userInput text] intValue];
whats pissing me off about this is that i did the same in a previous project and the "input worked" only, but now everything isn't.
and my property as i haven't defined anything.
i appreciate your help
I Think you have to set the frame for UITextField like this ...
userInput.frame=CGRectMake(100,100,60,31);
in your -(UITextField *)userInput method, that's y it didn't show ..
Well few things are hazy but ...
You are getting the value of the text field using,
int nInput = [[userInput text] intValue];
If you are not getting the correct value then nInput will be zero. That is because userInput must be nil. You can verify this by logging it by adding NSLog(#"%#", userInput); before the line above.
Whatever value is printed, it looks like your on screen text field is not referenced to by userInput.
Since you're doing the entire thing programmatically, you will have to add your userInput to the view hierarchy by doing this,
[self.view addSubview:self.userInput];
Using the property accessor is important here is important as otherwise userInput would be nil unless you've accessed it using self.userInput earlier.
Although this is tagged iPhone, do you happen to be simulating an iPad? If so, I have run into this. The iPad will not show a number pad keyboard.
userInput= [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 30)]
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
You can just set the frame and add below property(to visible the textfield frame in View otherwise it is not visible in View).
userInput.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:userInput];
I hope it will be helpful to you.