gpt4 book ai didi

iphone - 如何使用自己的委托(delegate)方法添加 subview ?

转载 作者:行者123 更新时间:2023-11-28 22:55:07 24 4
gpt4 key购买 nike

enter image description here

正如我的屏幕截图所示,我在 SliderValueViewController.xib 上有 UIButton。我希望当我单击此 UIButton 时,它会自动在 Secondview.xib 中添加 subview (名称为 detailview)。这是我的代码。在 SliderValueViewController.h 中我声明了我的协议(protocol),

#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;

这是我的按钮功能..

    -(IBAction)gotodetailview
{
[self.delegate displayview];
}

现在我的secondview.h

enter image description here

我的 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)gotodetailview)被调用但它没有调用委托(delegate)方法方法(-(void)displayview)在第二个 View 中。任何人都可以提前帮助我犯什么错误。谢谢。

最佳答案

secondview.m

您似乎忘记设置 SliderValueViewControllerdelegate 属性

在呈现或推送“SliderValueViewController”的函数中你会有这样的东西

SliderValueViewController *controller = [[SliderValueViewController alloc] init];
//You will need to add this code
//self is secondview
controller.delegate = self;

现在当 [self.delegate displayview]; 得到调用时,这将调用 secondview 中的 displayview

关于iphone - 如何使用自己的委托(delegate)方法添加 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com