iphone - 在事件处理程序中获取 UIButton 的标题

标签 iphone objective-c cocoa uibutton

我创建了一个按钮并将标题设置为“单击此处”。当我按下那个按钮时,我想得到那个按钮的标题并记录下来。这是我的代码,我哪里出错了?

-(void)clicketbutton {
    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [mybutton setTitle:@"Click here"  forState:UIControlStateNormal];
    [mybutton addTarget:self  
       action:@selector(displayvalue:)forControlEvents:UIControlEventTouchUpInside]; 
}

-(void)displayvalue:(id)sender {       
    UIButton *resultebutton= [UIButton buttonWithType:UIButtonTypeCustom];

    resultebutton=sender;// pls clear here.. my question here , it it possible or not. if possible how ?
    NSLog(@" The buttontitile is %@ ", [resultebutton.Title] // here also.
}

最佳答案

您的 displayvalue: 方法应如下所示:

-(void)displayvalue:(id)sender {       
    UIButton *resultButton = (UIButton *)sender;
    NSLog(@" The button's title is %@.",  resultButton.currentTitle);
}

(请查看 XCode 中的文档,它会给您正确的答案。)

关于iphone - 在事件处理程序中获取 UIButton 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/900867/

相关文章:

ios - NSTimeInterval取绝对值可以吗?

ios - objective-c - 获取指向对象属性的指针

objective-c - 在 NSMenuItem 中显示图标时没有缩进

objective-c - 来自 NSTextView 插入点的 NSpoint

iPhone 应用程序在启动时使用 60MB 虚拟内存

iphone - 如何在 ios 8 和 ios 7 中创建自定义弹出 View ?

ios - Xcode 中的第 3 方框架编译警告

objective-c - 编译/运行在 10.7.x 中实现 NSSharingServicePicker 的 Cocoa 项目

iphone - iOS -- 转换PDF导致颜色饱和

iphone - 替换出现的 NSString - iPhone