How to add a UIView to an UIAlertView? - objective-c

I have to create a custom AlertView with a view that contain some label. I create the .h and .m of CustomAlert
this is the CustomAlert.h.
#import <Foundation/Foundation.h>
#interface CustomAlert: UIView
#property (nonatomic, weak) IBOutlet UILabel *ok;
#property (nonatomic, weak) IBOutlet UILabel *ok1;
#property (nonatomic, weak) IBOutlet UILabel *ok2;
#property (nonatomic, weak) IBOutlet UILabel *ok3;
#property (nonatomic, weak) IBOutlet UILabel *ok4;
#property (nonatomic, weak) IBOutlet UILabel *ok5;
#end
and this is the CustomAlert.m.
#import "CustomAlert.h"
#implementation CustomAlert
#synthesize ok = _ok;
#synthesize ok1 = _ok1;
#synthesize ok2 = _ok2;
#synthesize ok3 = _ok3;
#synthesize ok4 = _ok4;
#synthesize ok5 = _ok5;;
#end
I also create the xib with just a view and i connect the label with all the "ok".
Now, i want to add this View to an AlertView with just an Ok button. How can i do it? I want to use it with ios 7 and 6.
Thanks!!

In iOS 7 you should not munge a UIAlertView like this - and there is no need, because thanks to custom view transitions you can make your own small presented view in front of your interface, so that it looks and acts like a UIAlertView but can contain anything you like.
I've written an online tutorial on how to do this:
http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html
And there's a downloadable github project that goes with it:
https://github.com/mattneub/custom-alert-view-iOS7

Related

Changing label's data using textField inout

I want to change the label's text (which is embedded in a tableViewCell) using a textField data (which is embedded outside the tableViewCell).
Can anyone share the code please using objective-C.
Here is my TableViewCell.h file.
#import <UIKit/UIKit.h>
#interface TableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#end
Here is my storyboard controller file
#import <UIKit/UIKit.h>
#interface ViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *contacts;
}
- (IBAction)leftBtn:(id)sender;
- (IBAction)rightBtn:(id)sender;
- (IBAction)nextBtn:(id)sender;
- (IBAction)SendBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tabV1;
#property (strong, nonatomic) IBOutlet UITextField *txtField;
#end
Here I have the button action connection where I should I write the actual code to change the label's data using a text field.
- (IBAction)leftBtn:(id)sender {
}

cocoa-Why there is an IBOutlet and a property that are of the same name?

I'm new to objective-c. When I'm reading some source code written by others, I encountered a problem.
I found that there is
IBOutlet NSPopover *popover;
as well as
#property NSPopover *popover;
PopoverViewController.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "TimerPopoverViewController.h"
#class TimerLogic;
#class TimerInfo;
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate> {
#private
IBOutlet NSPopover *popover;
IBOutlet NSWindow *detachWindow;
IBOutlet TimerPopoverViewController *viewController;
}
#property NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
I think they are different variables. However, I can't figure out what do they do?
As far as I'm concerned, the IBOutlet is to show a popover.
But what does the #property does?
This is either very old code or written in a very old (and now discouraged) style. The IBOutlet here is declaring an instance variable (ivar). The #property is declaring a property that is backed by the instance variable. In modern ObjC you should implement it this way:
PopoverViewController.h
#import <Cocoa/Cocoa.h>
#class TimerInfo;
// Things declared here are public
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate>
// You could leave this here if it is required by other parts of the program,
// but other parts of the program really shouldn't require it. See below.
// #property (nonatomic, readonly, weak) NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
PopoverViewController.m
// Generally avoid importing local headers into the .h unless you have to.
#import "TimerPopoverViewController.h"
// Things declared here are private. This is much better than the old #private.
#interface TimerPopoverDelegate ()
#property (nonatomic, readwrite, weak) IBOutlet NSPopover *popover;
#property (nonatomic, readwrite, weak) IBOutlet NSWindow *detachWindow;
#property (nonatomic, readwrite, weak) IBOutlet TimerPopoverViewController *viewController;
#end
(Currently popover is public, but you should avoid exposing an IBOutlet that way. Outside objects should not directly touch a view controller's outlets.)

How can I improve my attempt to create protected properties

#import "BGReviewController.h"
#interface BGReviewController (protected)
#property (weak, nonatomic) IBOutlet UIImageView *backgroundImage;
#property (weak, nonatomic) IBOutlet UIButton *dislikeButton;
#property (weak, nonatomic) IBOutlet UIButton *likeButton;
#property (weak, nonatomic) IBOutlet UIButton *post;
#property (weak, nonatomic) IBOutlet UIButton *cancel;
#property (weak, nonatomic) IBOutlet UITextView *textView;
#end
Then I add:
#synthesize backgroundImage = _backgroundImage;
#synthesize dislikeButton = _dislikeButton;
#synthesize likeButton = _likeButton;
#synthesize post = _post;
#synthesize cancel = _cancel;
#synthesize textView = _textView;
And I got error:
/business/Dropbox/badgers/BadgerNew/BGReviewController.m:32:13: Property declared in category 'protected' cannot be implemented in class implementation
Should I declare the synthesize in the implementation file of the category and then put the ivars explicitly?
you cant #synthesize in a category, you have to do the getters and setter manually
check out this answer
Replace
#interface BGReviewController (protected)
With
#interface BGReviewController ()

UITextField bind

I can't figure out why that code is not working for me.
ViewController.h
...
#property (nonatomic, copy) UITextField *textField;
...
ViewController.m
-(void)viewDidLoad
{
[self.textField addTarget:self
action:#selector(textIsChanged:)
forControlEvents:UIControlEventEditingChanged];
}
-(void)textIsChanged:(id)sender;
{
NSLog(#"Changed");
}
When I type something in the textField textIsChanged method is never invoked.
You should declare textField as an IBOutlet like this:
#property (nonatomic, retain) IBOutlet UITextField *textField;
or, if you are using ARC (Automatic Reference Counting):
#property (nonatomic, strong) IBOutlet UITextField *textField;
and bind it from the xib file in interface builder.

why wont it recognize the #properties from header file from main file?

UIView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface UIView : UIResponder {
IBOutlet UILabel *endLabel;
IBOutlet UIButton *goButton;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
}
- (IBAction)goButtonClicked;
#end
UIView.m
#import "UIView.h"
#implementation UIView
#synthesize textBox1, goButton;
#synthesize textBox2, goButton;
#synthesize textBox1, endLabel;
#synthesize textBox2, endLabel;
#synthesize goButton, endLabel;
- (IBAction)goButtonClicked {
}
#end
Going a bit crazy with the #synthesizes, are we? I do believe that your main problem here is that #property declarations need to be after the closing } of #interface.
I'm surprised the compiler didn't throw up a red flag the size of Greenland, tho'.
Additionally, you probably meant to make a custom subclass of UIView; I'll use MyView.
//MyView.m -- correct synthesize declaration
#synthesize textBox1, goButton, textBox2, endLabel;
//MyView.h -- correct interface declaration
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface MyView : UIView {
IBOutlet UILabel *endLabel;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
IBOutlet UIButton *goButton;
}
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
#end
The first problem is that you're naming your class UIView, which already exists in UIKit. See #Williham's advice on resolving this.
You only need one #synthesize per property, and when the property name matches the instance variable name, you should only need to do something like this in your .m file:
#synthesize endLabel;
#synthesize goButton;
#synthesize textBox1;
#synthesize textBox2;
Also, you're likely to run into problems getting your IBAction method to work. To use a method for a target-action linkage, it must have a return type of IBAction (which you have correct) and accept an id parameter representing the sender. The canonical method signature looks like this:
- (IBAction) goButtonClicked:(id)sender;
I'd actually recommend a method name that's not explicitly tied to the button that invokes it, especially since there could be other ways to invoke the same action. (For example, if you were writing a desktop application, a key equivalent or menu command could do the same thing.)