我通过子类化UIView创建了一个自定义选择器视图类型的控件.我希望能够从此控件中发送"UIControlEventValueChanged"控件事件,以便我可以在任何使用该控件的视图控制器中注册它.
当我认为应该触发此事件时,如何让我的自定义控件触发此事件?
我想在用户点击tabbar项时禁用默认操作.
例如,我有一个Tab5,Tab1,Tab2和Tab3.在Tab1中,用户可以从View1导航到View3(View1> View2> View3).如果用户在View3,并且他点击Tab1,则应用程序将用户带到View1(根视图控制器).我想禁用此功能.我不希望在Tab1上点击以弹出所有视图控制器.我怎样才能做到这一点?
编辑:
这种行为有点奇怪,但在深层次结构的情况下是一个方便的快捷方式!
您可以实现以下UITabBarControllerDelegate方法来禁用此系统范围的快捷方式:
#pragma mark -
#pragma mark UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
UIViewController *tbSelectedController = tbc.selectedViewController;
if ([tbSelectedController isEqual:vc]) {
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud) Apple文档说传递给NSMenuItem的动作的发送者可以设置为一些自定义对象,但我似乎无法弄清楚如何做到这一点.有没有一种方法我在文档中没有看到某个地方?
如果我为同一事件添加一个控件多个目标 - 操作对,控件是否会按照我添加它们的顺序将操作消息发送到目标?
我阅读了以下参考文献但未找到答案.
UIButton
从情节提要中从中创建事件处理程序时,Swift @IBAction
在之前添加func
。当以编程方式将事件添加UIButton
到时func
,Swift会出错,并说我需要@objc
在方法前面添加,但是当我添加时@IBAction
,它也会编译。
两者之间有区别吗?以UIButton
编程方式向我添加事件时应该使用哪一个?
我想要一个协议:
protocol CameraButtonDelegate: class {
func cameraButtonDidPress(_ sender: UIButton)
}
Run Code Online (Sandbox Code Playgroud)
因此,我可以将任何客户端分配给按钮,例如:
cameraButton.addTarget(delegate, action: #selector(cameraButtonDidPress), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
但是,它不能编译,因为我必须在中指定特定功能action
,例如:
cameraButton.addTarget(delegate, action: #selector(AAPLViewController.cameraButtonDidPress), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
如果我想通过一个按钮将多个客户作为目标,该如何解决此问题?
我需要从对象中删除一个动作,然后添加一个新动作.
我已使用此代码添加新操作:
[Button addTarget:self action:@selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用此代码删除旧操作:
[Button removeTarget:self action:@selector(oldAction:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
问题是它以某种方式也删除了newAction.
有任何想法吗?
提前致谢 :)
我想处理点击UICollectionView单元格.尝试使用以下代码实现此目的:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
// Some code to initialize the cell
[cell addTarget:self action:@selector(showUserPopover:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)showUserPopover:(id)sender
{
//...
}
Run Code Online (Sandbox Code Playgroud)
但执行中断[cell addTarget:...]
了以下错误:
- [UICollectionViewCell addTarget:action:forControlEvents:]:无法识别的选择器发送到实例0x9c75e40
ios ×3
objective-c ×3
cocoa-touch ×2
iphone ×2
swift ×2
uibutton ×2
cocoa ×1
delegation ×1
ibaction ×1
nsmenuitem ×1
tabbar ×1
uicontrol ×1
uikit ×1
uitabbar ×1