我正在尝试构建一个与 Twitter 集成的应用程序。当用户单击共享按钮时,此按钮应更改其图像。这已经有效,但是当用户单击其他视图并最终再次单击带有共享按钮的视图时……图像又是旧图像了。我不知道如何解决这个问题,所以我可以帮助你给我一些建议:)
这是代码:
在我的 .h 文件中:
- (IBAction)postToTwitter:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *tweetBTN;
...在我的 .m 文件中:
- (void)viewDidLoad
{
//some code here
if ([[UIImage imageNamed:@"twitter02_dark.png"] isEqual:_tweetBTN.currentImage]) {
UIImage *twitterImage2 = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage2 forState:UIControlStateNormal];
}
else {
UIImage *twitterImage1 = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage1 forState:UIControlStateNormal];
}
}
- (IBAction)postToTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
//some code here
UIImage *twitterImage2 = [UIImage imageNamed:@"twitter02_dark.png"];
[_tweetBTN setImage:twitterImage2 forState:UIControlStateNormal];
}
}
如果有人可以帮助我,那就太好了...
更新:
我在答案中进行了修改......但我没有t get one thing: after first launching the app the image should be active (because it isn
“发推”)......
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"tweeted"]) {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_dark.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
else {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
...但是图像很暗...为什么?之后这个 if 方法正常工作......对我来说很奇怪