Hide/Show & Enable/Disable a button ? - ios

I'm quite new at iOS development and I'm facing some trouble with hiding/showing button in my GUI. Because I need some buttons to appear or disappear and to be enabled or disabled. I followed some great tutorials over the net but can not figure out what is going wrong with my code.
Here is my ViewController.h :
/
// ViewController.h
// WeddingVideoBooth
//
// Created by Frédéric Mouza on 15/07/13.
// Copyright (c) 2013 Frédéric Mouza. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
IBOutlet UIButton *but_record;
}
#property (nonatomic,retain) IBOutlet UIButton *but_record;
- (IBAction)but_record:(UIButton *)sender;
#end
and my .m file :
//
// ViewController.m
// WeddingVideoBooth
//
// Created by Frédéric Mouza on 15/07/13.
// Copyright (c) 2013 Frédéric Mouza. All rights reserved.
//
#import "ViewController.h"
#import "MobileCoreServices/UTCoreTypes.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize but_record;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
but_record.hidden=YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)but_record:(UIButton *)sender {
but_record.enabled=NO;
}
#end
It's very simple and, to understand, I just would like the button to disable when you click on it... Currently, the button remains the same when you click on it. I also tried to hide it using the property "but_record.hidden=YES" but nothing worked.
Does somebody have an idea, please ?
Thanks again

Add an NSLog() into your but_record to see if the IBAction is actually getting called. It sounds like that is not triggering as you probably didn't link them together in your Interface Builder. And as mentioned above take out the hidden=YES

Ok, just to wrap-up and properly close the question.
It's probably obvious for most of you but when dealing with interface one has to be careful:
if you create a button, link it to the interface, give it properties... and then you copy it Xcode keeps the previous link in the copy and if you create a new link by control+drag in your .h file, the previous link remains and may supersede the new one.
Therefore, to prevent this, you have to remove the existing links from the link tab after you copied a button but before you create a new link.
That worked for me.
Hope this will help,
Fred

Related

Button and label are not shown in the iOS app using Xcode

I am trying to write a hello world app on Xcode but I have two errors
The first error message is DTAssetProviderService could not start DTXConnection with Simulator 'iPhone 5'. Check the system log for errors. but I still can run the app but when I run the app on simulatior it does not show the button and label I put in the app. It shows just a screen of the app. Here is my codes in the app for ViewController.h and ViewController.m
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UILabel *hellolabel;
}
- (IBAction)button1:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
-(IBAction)button1:(id)sender {
hellolabel.text = #"hello world";
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here is my main.storyboard interface
enter image description here
put your button and label in top - left side in your storyboard interface builder for now and you can see it on simulator.
It is because of different screen sizes, size classes and autolayout.
You should learn autolayout, after you can set constraints to your components and this kind of problem will not be faced.
The issue is probably in your main.storyboard, which you haven't linked below. More than likely, as others have said, you've mucked up your constraints.
On your main.storyboard delete any constraints and move all your buttons to the left most side. Also make sure that your width and height read wAny hAny, as that is the best to use for any screen.

Unwind Segue doesn't exit the view : Objective-C

I code with Xcode v6.1.1
I have two classes: Reports, and Admin.
When we are in Reports (first view), I tap on admin button, and the Admin view appears (second view).
When I tap on exit button (in Admin view), I would like that Admin view disappears and Reports comes back.
I don't know if I make error in code, but NSLog appears from prepareForUnwind.
Unwind segue appears in storyboard and link to "exit". Unwind segue identifier : "unwindToContainerVC" Action : prepareForUnwind:
Here is my code :
Admin.h:
#import <UIKit/UIKit.h>
#import "Reports.h"
#interface Admin : UIViewController
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue;
#end
Admin.m:
#import "Admin.h"
#interface Admin ()
#end
#implementation Admin
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
NSLog(#"Called unwind action");
}
#end
Have I to put something in Reports.h or Reports.m ?
Thanks !
Did you wire up the Exit Button to the "exit" action in your storyboard?
Make sure you followed all of the instructions here: Using Unwind Segues

Composing iOS Views

I've created a view with a logout button and I'm trying to make that a subview of another view. The logout button view has a xib and a controller associated with the xib.
How do I make it so that this view/controller is a part of my other view?
The way I've done this before is by having a view that draws itself programmatically, drawing that view in the interface builder as part of another view and changing the class for that view. As I want that view to respond to methods, I made it have a protocol and then made the controller it was a subview of implement that.
Is that the only way to do it? Or is there a way such that I have an independent controller for my logout view that I can just 'drop in' into other views, because the drawback of the other method is that every view that wants to use this subview has to implement the protocol, even if that method is going to be the same in every view.
Create a superclass to abstract the logout behavior. Then, each UIViewController that supports the logout should subclass that superclass. In the superclass, provide the method for logout.
This approach will enable you to either simply hook up UIControls in Interface Builder to the common IBAction in the superclass, or alternatively, even add specific customization in the subclass before invoking the superclass method.
Here's one possible example:
LogoutViewController.h
#import <UIKit/UIKit.h>
#interface LogoutViewController : UIViewController
-(void)performLogout;
#end
LogoutViewController.m
#import "LogoutViewController.h"
#interface LogoutViewController ()
#end
#implementation LogoutViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)performLogout
{
//do logout code
}
- (IBAction)logout:(id)sender
{
[self performLogout];
}
#end
SomeOtherViewController.h
#import <UIKit/UIKit.h>
#import "LogoutViewController.h"
#interface SomeOtherViewController : LogoutViewController
#end
SomeOtherViewController.m
#import "SomeOtherViewController.h"
#implementation SomeOtherViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)mySpecificLogoutButtonPressed:(id)sender
{
self.title = #"Good bye";
// do other code specific to logging out from this UIVC
[super performLogout];
}
#end
You can use NSNotificationCenter for this. So you can post the notification on logout button action. You can check the documentation.
Hope this helps.

Altering the UIButton behaviour by subclassing

I want to create a check box button which will be used throughout my application at many places. The one basic behaviour I wanted for this button was to alternatively changed its state when the button was tapped. So I subclassed this button and wrote the following class. Basically it just adds one extra target method to button in the -awakeFromNib function.
//
// CheckBoxButton.h
// CheckBoxButton
//
// Created by Ankit Srivastava on 11/07/13.
// Copyright (c) 2013. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface CheckBoxButton : UIButton
#end
here is the .m
/
// CheckBoxButton.m
// CheckBoxButton
//
// Created by Ankit Srivastava on 11/07/13.
// Copyright (c) 2013. All rights reserved.
//
#import "CheckBoxButton.h"
#implementation CheckBoxButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)awakeFromNib{
[self addTarget:self action:#selector(alterState:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) alterState:(UIButton*)sender {
[self setSelected:!self.isSelected];
}
#end
Now I add the button via xib and just change its base class to CheckBoxButton.
Every thing works fine but I have heard that UIButton should not be subclassed but I couldn't find any documented proof and also I am just adding a method to the button to change its state. So my question is whether this approach is OK..?
Thanks for your inputs.
I've subclassed UIButton several times. It's a class cluster so there are potential issues when doing so, but I have yet to run into any show stoppers.

error: "No visible #interface for 'UIAlertView' declares the selector 'initWithTitle'

I am in the process of reading the O'Reilly book Learning Cocoa with Objective-C 3rd edition.
The O'Reilly website doesn't have a forum for this specific book, and searching for this error returns nothing.
On page 18, I keep getting the following error:
"No visible #interface for 'UIAlertView' declares the selector 'initWithTitle:message:deluge:cancelButtonTitle:otherButton'"
Here's my code:
//
// ViewController.m
// HelloCocoa
//
// Created by ME on 1/14/13.
// Copyright (c) 2013 ME. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showAlert:(id)sender
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Hello!"
message:#"Hello, World!"
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitle:nil];
[alert show];
[_helloButton setTitle:#"I was Clicked!" forState:UIControlStateNormal];
}
#end
//
// ViewController.h
// HelloCocoa
//
// Created by ME on 1/14/13.
// Copyright (c) 2013 Andrew DiNatale. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *helloButton;
- (IBAction)showAlert:(id)sender;
#end
What's causing this error?
The otherButtonTitles parameter is plural (as in otherButtonTitle*s*).
UIAlertView is a UIKit (iOS) class. Looks like you are trying to use it in a Cocoa project, instead of Cocoa Touch.
I'm one of the authors on this book.
This question has already been correctly answered, but I just wanted to chime in - the answer from CodaFi is correct, the issue was that the method ends with "otherButtonTitles" (with an s), rather than "otherButtonTitle".
I just double-checked page 18 of the book, and it actually looks like the book had it correct!
If you have any other questions about the book, post 'em up here - I'll be floating around, looking for any questions that mention the book. Errata is always welcome!

Resources