如我的屏幕截图所示,我在SliderValueViewController.xib。上具有UIButton。我希望当我单击此UIButton时,它会在Secondview.xib。自动添加subview(名称为detailview)。这是我的代码。在SliderValueViewController.h中,我声明了我的协议,
#import <UIKit/UIKit.h>
@protocol Testmyview
-(void)displayview;
@end
@interface SliderValueViewController : UIViewController {
IBOutlet UILabel *sliderValue;
IBOutlet UIButton *btngo;
id <Testmyview> delegate;
}
@property (nonatomic, retain) IBOutlet UILabel *sliderValue;
@property (nonatomic, assign)id <Testmyview> delegate;
-(void)updateSliderValueTo:(CGFloat)_value;
-(IBAction)gotodetailview;
@end
并在SliderValueViewController.m中
#import "SliderValueViewController.h"
#import"secondview.h"
@implementation SliderValueViewController
@synthesize sliderValue;
@synthesize delegate;
这是我的Button功能。
-(IBAction)gotodetailview
{
[self.delegate displayview];
}
现在我的secondview.h
我的secondview.m看起来就像这样。
#import "secondview.h"
#import "SliderValueViewController.h"
#import "ELCSlider.h"
@implementation secondview
这是我想要的功能。
-(void)displayview
{
[self.view addSubview:detailview];
}
在ELSLiderl.m中,我这样做。
-(id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
sliderValueController = [[SliderValueViewController alloc] initWithNibName:@"SliderValueViewController" bundle:[NSBundle mainBundle]];
popoverController = [[UIPopoverController alloc] initWithContentViewController:sliderValueController];
[popoverController setPopoverContentSize:sliderValueController.view.frame.size];
}
return self;
}
-(void)valueChanged {
[sliderValueController updateSliderValueTo:self.value];
CGFloat sliderMin = self.minimumValue;
CGFloat sliderMax = self.maximumValue;
CGFloat sliderMaxMinDiff = sliderMax - sliderMin;
CGFloat sliderValue = self.value;
if(sliderMin < 0.0) {
sliderValue = self.value-sliderMin;
sliderMax = sliderMax - sliderMin;
sliderMin = 0.0;
sliderMaxMinDiff = sliderMax - sliderMin;
}
CGFloat xCoord = ((sliderValue-sliderMin)/sliderMaxMinDiff)*[self frame].size.width-sliderValueController.view.frame.size.width/2.0;
CGFloat halfMax = (sliderMax+sliderMin)/2.0;
if(sliderValue > halfMax) {
sliderValue = (sliderValue - halfMax)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
sliderValue = sliderValue*11.0;
xCoord = xCoord - sliderValue;
}
else if(sliderValue < halfMax) {
sliderValue = (halfMax - sliderValue)+(sliderMin*1.0);
sliderValue = sliderValue/halfMax;
sliderValue = sliderValue*11.0;
xCoord = xCoord + sliderValue;
}
[popoverController presentPopoverFromRect:CGRectMake(xCoord, 0, sliderValueController.view.frame.size.width, sliderValueController.view.frame.size.height) inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
使用所有这些代码,它不会给我带来任何磨损或错误,但是当我使用断点时,它没有添加subview。当我在第二视图中调用UIButton方法(-(IBAction)gotodetailtail)但未调用委托方法方法(-(void)displayview)时,却没有添加subview。谁能帮助我提前犯.thanx的错误?
最佳答案
在secondview.m
中
您似乎忘记了设置delegate
的SliderValueViewController
属性
在显示或推送“ SliderValueViewController
”的功能中
你会有这样的事情
SliderValueViewController *controller = [[SliderValueViewController alloc] init];
//You will need to add this code
//self is secondview
controller.delegate = self;
现在,当
[self.delegate displayview];
接到电话时,它将在displayview
中呼叫secondview
关于iphone - 如何使用自己的委托(delegate)方法添加 subview ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10960774/