Why won't my UIButton work? - iphone

I'm trying to call Sharekit with a UIButton, but when I tap the button running on my device, nothing happens.
This should be the relevant code in my view controller's .h
#import "SHK.h"
#import "SHKFacebook.h"
#interface ViewController : UIViewController <CaptureManagerDelegate>
{
IBOutlet UIButton *upload;
}
#property (nonatomic, retain) IBOutlet UIButton *upload;
-(IBAction)buttonPressed:(id)sender;
And in my view controller's .m (using code from example here http://gazapps.com/wp/2011/07/17/basic-sharekit-customization)
#synthesize upload;
-(IBAction)buttonPressed:(id)sender {
SHKItem *item;
NSURL *url = [NSURL URLWithString:#"http://www.gazapps.com"];
item = [SHKItem URL:url title:#"Checkout this site"];
[SHKFacebook shareItem:item];
}
-(void)dealloc
{
[upload release];
}
Besides trying to use a UIButton instead of UIBarButton, I've followed Sharekit's install instructions to the letter. In my storyboard, I CTRL-dragged to the "first responder" and linked up my button to buttonPressed. I'm not getting any warnings or errors in xcode. But nothing happens when I tap the button.

You code seems fine. The problem is probably within the nib file. Check your connections again.
Are you doing anything else with your upload (UIButton) property? You don't need that Outlet at all, if you only want to trigger one IBAction.
You could put a breakpoint in your buttonPressed method, to see if the connections are setup correctly.

you should use SHKActionSheet
SHKActionSheet *actionSheet = [EDShareActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:self.navigationController.toolbar];

Related

Clicking a button in UIActionSheet causes my app to crash

I'm trying to implement a UIActionSheet which will have some sharing buttons on it for sharing to twitter, facebook, email and print.
Here is my share view controller.h
#interface ShareViewController : UITableViewController <UIActionSheetDelegate, MFMailComposeViewControllerDelegate>
#property (nonatomic, strong) NSMutableDictionary *services;
#property (strong, nonatomic) UIActionSheet *actionSheet;
#property (strong, nonatomic) id <ShareViewControllerDelegate> delegate;
#property (nonatomic, strong) UIPopoverController *popCon;
#property (nonatomic, strong) NSString *serviceTitle;
-(IBAction)loadActionSheet:(id)sender;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)tweetThis;
- (void)printThis;
- (void)openMail;
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;
#end
Here is my share view controller.m
-(IBAction)loadActionSheet:(id)sender{
if (self.actionSheet) {
// do nothing
}
else {
UIActionSheet *sharing = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"Twitter", #"Facebook", #"Email", #"Print", nil];
[sharing showFromBarButtonItem:sender animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button index: %d",buttonIndex);
switch (buttonIndex) {
case 0:
[self tweetThis];
break;
case 1:
[self sendToFacebook];
break;
case 2:
NSLog(#"email");
[self openMail];
break;
case 3:
NSLog(#"Print");
[self printThis];
break;
default:
NSLog(#"Error");
break;
}
}
I ma initiaalising my share view controller in main view controller.m as follows:
- (IBAction)shareButtonTapped:(id)sender {
ShareViewController *svc = [[ShareViewController alloc] initWithStyle:UIActionSheetStyleDefault];
[svc loadActionSheet:(id)sender];
}
I can open the action sheet fine, but when I click on any of the buttons, it crashes the app. Any ideas as to why?
This is most likely a memory-management issue. It looks like you're using ARC, so after the shareButtonTapped method returns, ARC will automatically release svc, as it's no longer referenced anywhere. The delegate property of the action sheet is weak, so it's not retained by that either.
I don't really see why ShareViewController is a view controller in the first place, as you never seem to be loading its view, but you may have your reasons... In any case, you should make that controller a strong property or instance variable of your other (main) view controller, so that it's not automatically released before the action sheet finishes.
Yes. Go to your .xib file.
You should see all the buttons you have.
Click on a button and go to the "Connections Inspector"
Delete the referencing outlet.
Drag the referencing outlet to the button and set the button to the "ID" you want.
Let me know if that works.
Just a stupid answer - the methods like - (void) sendToFacebook; are implemented, right?

Trying to disable button using button.enabled = false, but having no luck

I'm getting frustrated because I am failing to have any control over my bar button items or my UIToolbar. I am trying to disable a UIBarButtonItem, but it continues to respond to touch events. Here is what I have done, the code is so simple I don't know why it isn't working.
in my .h:
IBOutlet UIBarButtonItem *button;
#property (nonatomic,retain) IBOutlet UIBarButtonItem *button;
and in .m:
#synthesize button;
-(void)function{
button.enabled = false;
}
Am I doing something wrong with the viewcontroller delegate? I don't understand why I get no response. Thanks for your help.
This is correct:
button.enabled = false;
So are you sure that this method is actually being called? (And if this is your actual code, you have a method called 'function'?)
You might be able to achieve this with:
button.target = nil;
button.action = nil;
More:
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html
did you connect the button in the xib file to the outlet in the .h file? You may have set the target/IBAction, but you need to setup the reciprocal path. i.e. dragging from the File Owner, which is most likely the viewController, to the button. Then selecting the name of the IBOutlet in your .h file.
Good luck

How to assign multiple UIButtons to a UITextView

I want to assign two UIButtons to UITextView so that when one of the buttons is pressed the textview content should change from what it had when the previous button was pressed.
MyViewController.h:
#interface MyViewController : UIViewController
{
NSString* savedText;
}
#property(nonatomic, retain) IBOutlet UITextView* textView;
- (IBAction)buttonOnePressed:(id)sender;
- (IBAction)buttonTwoPressed:(id)sender;
#end
Connect the view controller's textView outlet to the text view in the xib file. Connect the buttons to the corresponding actions. (See Apple's Xcode tutorial if you don't know how to do this.)
MyViewController.m:
- (void)buttonOnePressed:(id)sender
{
[savedText release];
savedText = [textView.text copy];
}
- (void)buttonTwoPressed:(id)sender
{
textView.text = savedText;
}
(These aren't the complete files, of course, just the bits to make it do what you're asking.)

Interface builder connections cause crash in tab bar application

for exercise I'm trying to develop a simple iPhone application based on a tab bar from scratch, but I had some problem..
I've follow up these steps: http://www.techotopia.com/index.php/Creating_an_iPhone_Multiview_Application_using_the_Tab_Bar
The app loads correctly the three views until there's no connection within; if I connect any outlets or action to a label or button of the subviews, the app crash.
For example, my firstView.h contains:
#import <UIKit/UIKit.h>
#interface firstView : UIViewController {
IBOutlet UILabel *resultLabel;
}
-(IBAction)randomizeAction;
#property (nonatomic , retain) IBOutlet UILabel *resultLabel;
#end
and the firstView.m
-(IBAction)randomizeAction:(id)sender
{
//NSInteger rand = arc4random() % 75;
//resultLabel.text = [ [NSString alloc] initWithFormat:#"Random integer: #%",rand];
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:#"Yeah!" message:#"Yeah" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil ];
[alert show];
[alert release];
}
If I connect a label to the resultOutlet or a button to the "randomizeAction" method, the app crash when I switch to the related views..
Where is the mistake?
in .h file you declare a property to be IBOutlet twice. As I don't think this causes the problem but it's just unnecessary. Also you declared randomizeAction in the .h file wihout the parameter and you implemented it with a parameter.
Your code should look like this:
#import <UIKit/UIKit.h>
#interface firstView : UIViewController {
UILabel *resultLabel;
}
#property (nonatomic , retain) IBOutlet UILabel *resultLabel;
-(IBAction)randomizeAction:(id)sender;
#end
and the firstView.m
-(IBAction)randomizeAction:(id)sender
{
//do stuff
}
From that error message it looks like your view controller isn't hooked up properly in Interface Builder. Are you sure your view controller (File's Owner in IB) is identified as an instance of firstView? Check what class it is in the Identity Inspector in XCode.
Also it would be helpful if you can show the code where your view controller is declared and created (most likely in the App Delegate).
I'm not sure, but I think you have to change this:
#interface DetailViewController : UIViewController
To:
#interface firstView : UIViewController

IBAction is not shown in IB?

i have done like
#interface tabViewController : UIViewController
{
IBOutlet UIButton* button_first;
IBOutlet UIButton* button_second;
IBOutlet UIButton* button_third;
IBOutlet UIButton* home;
}
but i could not in file owner those button? what i have to do? any help please? i have set everything correctly (viewcontroller, view)...the day before it was working...i tried in interface builder to reload classes etc...why it happens?
You should have this:
VC Header file:
#interface tabViewController : UIViewController
{
IBOutlet UIButton* button_first;
IBOutlet UIButton* button_second;
IBOutlet UIButton* button_third;
IBOutlet UIButton* home;
}
- (IBAction) ButtonMethod;
VC .m file:
- (IBAction) ButtonMethod
{
// Code goes here
}
You should now be able to link to the "ButtonMethod" in Interface Builder. Right click the button, click on the circle next to "Touch Up Inside" and link it to "ButtonMethod".
Hope that helps.
EDIT:
This images doesn't show the blue line that appears when selecting but shows what you have to press:
Did you declare them as properties?
Using:
in .h-fle
#property(nonatomic, retain) UIButton *button_first;
And in .m-file:
#synthesize button_first?
?
have a look at the nib file. may be you have changed the class of that nib file.
Check out whether you see tabViewController in fornt of File's owner.
I am sure you have changed the class so the change is not getting reflected of IBOUTLET in files owner.
and as told by #GUBB its not at all required to set hte property and synthesis of the stuffs..
hAPPY cODING...