English 中文(简体)
X 说明如何从习俗类别中收回利得价值
原标题:Xcode how to retrieve slider value from custom class
  • 时间:2012-04-30 09:40:31
  •  标签:
  • cocoa
  • uikit

I am trying to draw a graph which moves up and down depending on the value of a slider. My graph is being drawn in a view which belongs to a custom class GraphView. There is one ViewController for the project and the slider calls a method moveLine. This has a property endXPoint which I have set such that: endXPoint = mySlider.value

我的问题是,我不知道如何在我《图表评论》的读法中参考这一价值。

I have tried creating a reference to GraphView in the ViewController and setting the property there but it does not work:
GraphView *myGraphView = (GraphView *)self.view;
myGraphView.endXPoint = mySlider.value;

最佳回答

您必须为图科文类设定一个财产,如:

@interface GraphView : UIView
@property float endXPoint;

接着,从主计长的角度,你将图表显示的“意见”变量定为:

[myGraphView setendXPoint: [mySlider value]];
[myGraphView setNeedsDisplay];

The last line ask GraphView to update the View, calling the drawRect method. In the drawRect method you can use endXPoint directly because it is a class property.

这是正确的版本:

//ViewController.h

#import <UIKit/UIKit.h> 
#import "GraphView.h" //import headers in the header file

@interface ViewController : UIViewController  

@property (strong, nonatomic) IBOutlet UISlider *mySlider;  
@property (strong, nonatomic) IBOutlet UILabel *myLabel;  
@property (strong, nonatomic) IBOutlet GraphView *myGraphView; //Connect this with the IB

- (IBAction)moveLine:(id)sender; 
- (IBAction)setLabelText:(id)sender;  
@end  

//ViewController.m

#import "ViewController.h"   

@implementation ViewController  
@synthesize mySlider;  
@synthesize myLabel;  
@synthesize myGraphView;



- (IBAction)moveLine:(id)sender {  
    [myGraphView setendXPoint:[mySlider value]];  
    [myGraphView setNeedsDisplay];  
}  

@end 

//GraphView.h 

#import <UIKit/UIKit.h>  

@interface GraphView : UIView  
@property float endXPoint;  


@end

//GraphView.m  

#import "GraphView.h"   

@implementation GraphView  
@synthesize endXPoint;  


- (void)drawRect:(CGRect)rect  
{

    CGContextRef ctx = UIGraphicsGetCurrentContext(); //get the graphics context  
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1);   
    CGContextMoveToPoint(ctx, 0, 0);  
    //add a line from 0,0 to the point 100,100;   
    CGContextAddLineToPoint( ctx, endXPoint,100);  
    //"stroke" the path  
    CGContextStrokePath(ctx);  
}  


@end
问题回答

暂无回答




相关问题
Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

How to get a flash url in a webpage using a webframe?

As we know , When we load frame from webpage of safari, we will invoke the delegate methods of webkit informal protocol(WebFrameLoadDelegate): webView:didStartProvisionalLoadForFrame: webView:...

热门标签