突出显示objective-C中其他类的按钮,iOS
我有一个应用程序,其中包括一个蓝牙LE类,用于处理BT连接。当特征值改变时,我希望BT类中的代码改变主viewcontroller上的按钮状态。我是obj-c新手,通常使用stackoverflow来帮助理解。我在viewcontroller中定义了以下内容:突出显示objective-C中其他类的按钮,iOS,ios,objective-c,uiviewcontroller,uibutton,Ios,Objective C,Uiviewcontroller,Uibutton,我有一个应用程序,其中包括一个蓝牙LE类,用于处理BT连接。当特征值改变时,我希望BT类中的代码改变主viewcontroller上的按钮状态。我是obj-c新手,通常使用stackoverflow来帮助理解。我在viewcontroller中定义了以下内容: @interface myViewController : UIViewController<CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate
@interface myViewController : UIViewController<CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate, UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIButton *eraseButton;
及
在BT类中,我尝试了很多事情(更改viewcontroller的属性,该属性通过计时器更改按钮状态,调用直接更改按钮状态的方法等),尽管代码已执行(我在myViewController中的代码上设置了断点),但按钮状态没有更改。例如:
myAppDelegate *app = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
app.vCtrl=[[myViewController alloc] init];
[app.vCtrl deselectEraseButton];
我做错了什么?最好的方法是什么?我肯定不会这么难
更新:
在蔡志伟(谢谢)的帮助下,我做了一些改变,但是唉,它仍然不起作用:
我在myAppDelegate.h中添加了以下内容:
@property (strong, nonatomic) myViewController* vCtrl;
我在myAppDelegate.m中添加了以下内容:
-(void)eraseButton:(BOOL)state{
[vCtrl accessEraseButton:state];
}
在BT课程中,我做了以下工作:
[self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:YES];
-(void) accessEraseButton: (BOOL) state
{
if (state==0)
{
[self.eraseButton setSelected:NO];
[self.view setNeedsDisplay];
}
else
{
[self.eraseButton setSelected:YES];
}
}
在该类中使用以下方法:
-(void) test
{
myAppDelegate *app = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[app eraseButton:0];
}
这称为myViewController,在thread1上执行了以下操作:
[self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:YES];
-(void) accessEraseButton: (BOOL) state
{
if (state==0)
{
[self.eraseButton setSelected:NO];
[self.view setNeedsDisplay];
}
else
{
[self.eraseButton setSelected:YES];
}
}
然后我添加了[self.view setNeedsDisplay]代码>祝你好运。按钮仍然没有改变状态,但代码正在运行(我可以点击一个断点,它在方法上中断,而方法在线程1上运行)?!?有什么想法吗
*更新2*
好的,我已经知道我做错了什么。我在没有正确引用viewcontroller的情况下执行了上述所有操作。因此,尽管代码正在执行,但viewcontroller的实例不正确,所有按钮句柄都为零。我需要在viewDidLoad中将以下内容添加到myViewController.m中:
myAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.myViewController = self;
现在它起作用了。快乐的日子…因为当你做app.vCtrl=[[myViewController alloc]init]
,您实际上创建了一个新的UIViewController
实例,而不是屏幕上的原始实例
我没有测试它们,但您可以通过以下方式实现您想要的:
- 通过界面生成器:在
myAppDelegate
中设置UIViewController
的IBOutlet
,将其连接到界面生成器中的ViewController,然后在myAppDelegate
中创建一个方法来更改它或直接使用myAppDelegate
作为属性访问它。现在,您可以在BT类中调用它,例如[app deseleasebutton]
或[app.myViewController deseleasebutton]
,如果按钮是AppDelegate/VC的属性,甚至可以对按钮执行某些操作
- 通过委托:创建一个
@协议
为BT类设置一个委托,以便it可以在其他类(如myAppDelegate
)内进行回调
- 通过NSNotificationCenter:使用
-(void)addObserver:(id)notificationObserver选择器:(SEL)notificationSelector名称:(NSString*)notificationName对象:(id)notificationSender
和-(void)postNotificationName:(NSString*)notificationName对象:(id)notificationSender用户信息:(NSDictionary*))用户信息
来执行此操作。这将适用于整个应用程序
我相信上面三个已经有大量的代码示例,所以只要搜索它们,你就会得到你需要的。我已经通过原始问题更新了两个更新,最终获得了理解和解决方案。希望它能帮助那些同样困惑的人 非常感谢您的帮助和快速回复。我认为可能缺少的部分是myAppDelegate中的方法代码。我这样做了,也遇到了同样的问题——代码在myViewController中执行,但按钮状态没有改变。为什么代码可以执行,但按钮的状态不变?我也尝试过notificationcenter方法,但这似乎有点慢-该按钮仅在BT方法完成5-10秒后才更改状态。检查是否在主线程中进行了所有UI更改?或者尝试使用setNeedsDisplay
进行强制更新,看看是否有帮助。