Objective c 将滑块值传递给子视图
我在主视图中放置了一个Objective c 将滑块值传递给子视图,objective-c,uiview,Objective C,Uiview,我在主视图中放置了一个UISlider,并添加了一个子视图UIView(通过界面生成器),该子视图与自己的类SecondView关联。我需要将滑块的值传递给我的子视图,以便在滑块更改时移动子视图中的点 我已经对原始代码进行了更改,下面的段落不再准确。我使用了@MatthiasBauch提供的建议更改 我认为这将是一个简单的问题,分享一个iVar之间的两个。我在我的ViewController界面中使用@property创建了一个iVarmyPoint(如果这仍然被视为iVar),在我的iActi
UISlider
,并添加了一个子视图UIView
(通过界面生成器),该子视图与自己的类SecondView
关联。我需要将滑块的值传递给我的子视图,以便在滑块更改时移动子视图中的点
我已经对原始代码进行了更改,下面的段落不再准确。我使用了@MatthiasBauch提供的建议更改
我认为这将是一个简单的问题,分享一个iVar之间的两个。我在我的ViewController
界面中使用@property
创建了一个iVarmyPoint
(如果这仍然被视为iVar),在我的iAction
中的ViewController
实现中设置滑块值更改时的myPoint=sliderValue.value
。然后在我的SecondView
实现中,我导入“ViewController.h”然后在我的SecondView
实现中调用我的call my iVar,但按照我的方式,它只返回nil
或0
,而不是滑块值
我不想使用全局变量
我看过另一篇文章,它似乎在问一个类似的问题,但我想我仍然没有理解这个概念。我的代码如下
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
@interface ViewController : UIViewController
{
SecondView *secondView; // Do I need this with secondView declared as a @property below?
}
@property (nonatomic, retain) SecondView *secondView;
- (IBAction)sliderChanged:(id)sender;
@property (weak, nonatomic) IBOutlet UISlider *sliderValue;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIView *myView;
@end
#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (assign, nonatomic) CGFloat thePoint;
@end
SecondView.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
@interface ViewController : UIViewController
{
SecondView *secondView; // Do I need this with secondView declared as a @property below?
}
@property (nonatomic, retain) SecondView *secondView;
- (IBAction)sliderChanged:(id)sender;
@property (weak, nonatomic) IBOutlet UISlider *sliderValue;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIView *myView;
@end
#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (assign, nonatomic) CGFloat thePoint;
@end
ViewController有一个名为
@myView
的属性,该属性指向运行时创建的SecondView实例
因此,ViewController的实现可以调用myView
上的drawRect:
方法并传入相关坐标
下面是一些用于ViewController的伪代码
- (IBAction)sliderChanged:(id)sender
{
myPoint = sliderValue.value;
myLabel.text = [NSString stringWithFormat:@"%.2f", myPoint];
[myView drawRect:CGRectMake(myPoint, 100, 4, 4)];
}
但是请注意,在ViewController中,myView
只是一个UIView,而不是SecondView。对于如何解决此问题,您有一些选择。最干净的方法可能是从SecondView中删除#import ViewController.h
,相反。也就是说,让ViewController执行导入SecondView.h的,然后将myView
升级到SecondView
如果由于某种原因(例如,如果XCode要求它严格保持UIView),那么您仍然可以在运行时向上转换myView
属性,如下所示:
[(*SecondView)myView drawRect:CGRectMake(myPoint, 100, 4, 4)];
或者,您可以将滑块的动作指向myView,就像此问题的答案针对按钮一样:
在编程中,通常有几种可能的解决方案。这取决于你选择最好的一个给你。祝你好运 告诉视图在哪里绘制,不要问(新分配的)viewController在哪里绘制
您正在分配一个新的viewController,此viewController当然不知道您在viewController中设置的值,您在其中实际更改了滑块。
这行不通。这两个ViewController不是同一个实例。他们同班,但仅此而已。来自一个viewController实例的值不会神奇地出现在第二个实例中
- (void)drawRect:(CGRect)rect
{
float aPoint = self.point;
UIBezierPath *point = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(aPoint, 100, 4, 4)];
[point fill];
}
相反,应该在滑块动作中设置次视图的点属性
大概是这样的:
将float@属性添加到视图中
@interface SecondView : UIView
@property (assign, nonatomic) CGFloat point;
@end
若要绘制,请使用该点,而不是新ViewController实例中的初始值
- (void)drawRect:(CGRect)rect
{
float aPoint = self.point;
UIBezierPath *point = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(aPoint, 100, 4, 4)];
[point fill];
}
并在滑块动作中设置次视图的点特性
- (IBAction)sliderChanged:(id)sender
{
secondView.point = sliderValue.value;
[secondView setNeedsDisplay]; // you might be able to omit that
myLabel.text = [NSString stringWithFormat:@"%.2f", myPoint];
}
1.无需道歉;-)2.如果您省略了所有您自己没有实现的方法,那么您的问题会更具可读性。实际上没有必要发布initWithFrame:
或didReceiveMemoryWarning
@MatthiasBauch的默认实现,代码已更新。我错过了什么?您可能还没有创建(或在interface builder中连接)secondView
。添加NSParameterAssert(第二视图)代码>在视图中加载。如果此断言失败(即应用程序在此行崩溃),则表示secondView
为零,因为它未在Interface Builder中连接,而且您自己也没有创建它。@MatthiasBauch,是的,这显然是我的问题。如何在interface builder中连接它?它不是连接到我的UIView的IBOutlet吗?我是否有点偏离了这一点?@MatthiasBauch,我在interface builder中放置了一个UIView,并将UIView的自定义类更改为secondView。我还需要做点别的吗?drawRect:
的rect参数与您认为的不一样;然而,我的滑块最终将控制一个相当复杂的数学问题,我不想a)重复代码,b)增加对内存和设备的额外需求。我喜欢这种方法,我使用了您提供的代码,但我肯定错过了一些东西,因为它仍然在重新调整零。我将更新上面的代码以反映我当前的代码。