我在ViewController中有一个方法来绘制一个按钮。在ViewController2中,我想调用方法并绘制按钮。
在ViewController.h中
@interface ViewController : UIViewController
-(void)method;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)method{
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[self.view addSubview:Touch1];
NSLog(@"hi ");
}
-(void)TouchButton1:(UIButton*)sender{
NSLog(@"hi again");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后我试图从ViewController2调用
- (void)viewDidLoad {
[super viewDidLoad];
ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method];
}
NSLog显示正确的文本但未创建任何按钮。 我的问题是什么?
谢谢
答案 0 :(得分:1)
使用可从两个视图控制器调用的类方法创建一个单独的类(例如,名为Utils
):
@implementation Utils
+(void)methodForView:(UIView *)view
{
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[view addSubview:Touch1];
}
@end
并称之为:
- (void)viewDidLoad {
[super viewDidLoad];
[Utils methodForView:self.view];
}
或者更好的是,在UIViewController
- 子类中实现该方法,然后从该基类派生所有其他视图控制器。
答案 1 :(得分:1)
我认为这个问题是由于" self"关于方法"方法"。您的按钮已添加到de firstView而不是第二个View中。要做你想做的事,你必须通过你想要添加按钮的范围。就像@trojanfoe给出的样本一样。
ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method:self.view];
在第二个视图中,您可以致电:
Section "Uninstall"
Call un.XXXXXXXXX
ExecWait '"$INSTDIR\f2p_ping.exe" --f2p' $0
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp"
DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "MyApp"
DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "MyApp"
DeleteRegKey HKLM "SOFTWARE\MyApp"
DeleteRegKey HKCU "Software\MyApp\Overlay\ExcludedProcesses"
DeleteRegKey HKCR "MyApp"
RMDir /r "$INSTDIR"
RMDir /r $PROGRAMFILES\MyApp
RMDir /r "$APPDATA\MyApp\*.*"
; Remove shortcuts, if any
SetShellVarContext current
Delete "$SMPROGRAMS\MyApp\*.*"
Delete "$SMPROGRAMS\Startup\MyApp.lnk"
SetShellVarContext all
Delete "$SMPROGRAMS\MyApp\*.*"
Delete "$SMPROGRAMS\Startup\MyApp.lnk"
Delete "$DESKTOP\MyApp.lnk"
; Remove directories used
SetShellVarContext current
RMDir "$SMPROGRAMS\MyApp"
SetShellVarContext all
RMDir "$SMPROGRAMS\MyApp"
RMDir "$INSTDIR"
SectionEnd
我认为这就是问题,我希望能帮助你