Kru*_*nal 4 iphone uibutton xib ipad ios
我做了一个简单的应用程序,我.xib在每个方向上加载不同,
我有两个XIB的Portxib和Landxib,在这两个xibs,我拖着一个按钮和分配
像1两个xib 一样标记它的值.
我在我的代码中写了这段代码 viewDidLoad
UIButton *btn=(UIButton *)[self.view viewWithTag:1];
[btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
这是选择器方法,
-(void)BtnClick:(id)sender
{
NSLog(@"BtnClick");
}
Run Code Online (Sandbox Code Playgroud)
UIButton的点击事件没有被解雇......
要么
有没有其他方法从两个方向调用相同的按钮方法?
编辑:
.h file
.m file

确保...
xib因为您在其中找到它self.view或确保它被放置在正确的视图中并使用该视图viewWithTag编辑
我觉得viewDidLoad你在设置代码的地方没有得到认可.所以只需在加载nib后注释掉代码viewDidLoad并转换你的changeOrientation方法.
-(void)changeOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation==UIInterfaceOrientationPortrait || orientation==UIInterfaceOrientationPortraitUpsideDown)
{
[[NSBundle mainBundle] loadNibNamed:@"PortraitXib" owner:self options:nil];
}
else
{
[[NSBundle mainBundle] loadNibNamed:@"LandscapeXib" owner:self options:nil];
}
//Adding this code here makes sense that nib is loaded and after that we are recognizing button from loaded view of nib.
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
[btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
在头文件中添加
IBOutlet UIButton *btn;
Run Code Online (Sandbox Code Playgroud)
从你的xib文件中引用这个btn.
在您的情况下,您使用的btn未从xib文件中引用,因此该方法未触发
-(IBAction)BtnClick:(id)sender
{
NSLog(@"BtnClick");
}
Run Code Online (Sandbox Code Playgroud)
使函数成为IBAction并从nib btn引用它.在这种情况下,您无需显式添加目标.
如果你没有在笔尖中添加按钮,那么不要忘记init和addsubview到视图
您可以在http://www.idev101.com/learn/interface_builder_connections.html上看到本教程
编辑 检查什么是打印?
for (UIButton *but in [self.view subviews])
{
NSLog("but.tag %d",but.tag);
if(but.tag==1)// compare for same row elements
{
NSLog("Button Added");
[btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13473 次 |
| 最近记录: |