I followed this tutorial on how to add custom info window to a google map marker,
in the UIView I've added a button and created an IBAction but when I click on it nothing happen
my infoWindow view code looks like this
.h
#import <UIKit/UIKit.h>
#import "Details.h"
#interface MarkerInfoWindowView : UIView
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UIButton *btn1;
- (void) initializeWithDetails:(Details*)p_details;
#end
.m
#import "MarkerInfoWindowView.h"
#implementation MarkerInfoWindowView
- (void) initializeWithDetails:(Details*)p_details
{
if(self != nil)
{
self.imageView.image = [UIImage imageWithContentsOfFile:p_basicDetails.imageURL];
self.label1.text = p_details.l1;
self.label2.text = p_details.l2;
}
}
-(IBAction) btn1_Clicked:(id)sender
{
NSLog(#"button clicked");
}
#end
and then in my view controller of the main screen and map
-(MarkerInfoWindowView*) customInfoWindow
{
if(_customInfoWindow == nil)
{
_customInfoWindow = [[[NSBundle mainBundle] loadNibNamed:#"MarkerInfoWindowView" owner:self options:nil] objectAtIndex:0];
}
return _customInfoWindow;
}
- (UIView *)mapView:(GMSMapView *)p_mapView markerInfoWindow:(GMSMarker *)p_marker
{
Details* temp = [[Details alloc] init];
temp.l1 = #"L1";
temp.l2 = #"L2";
temp.imageURL = #"someImage.jpg";
[self.customInfoWindow initializeWithDetails:temp];
return self.customInfoWindow;
}
any suggestions?
first the reason for the button not being clicked is because google-maps takes the UIView and renders it as an imageView so the button is part of an image and of course not clickable.
the solution is to add a UIView and handle on your own the hide/show and positioning.
instead of using
(UIView *)mapView:(GMSMapView *)p_mapView markerInfoWindow:(GMSMarker *)p_marker
i used didTapMarker and returned YES;
Related
I have a ViewController called HomeViewController. Its subview is ActivityView (UIView). ActivityView's subview is ActivityCard (UIView). In ActivityCard, I have a button called Enter. On it's click I need to send the values of all textfields from ActivityCard xib to HomeViewController and call an API from there. I have never worked with xib's before so unable to manage this scenario. Any help would greatly be appreciated!
EDIT:
ActivityCard.h
#interface ActivityCard : UIView
- (IBAction)actionEnterClick:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *lblActivityCount;
#property (weak, nonatomic) IBOutlet UIButton *enterButton;
#end
ActivityView.h
#interface ActivityView : UIView
#property (strong) ActivityCard *card;
#property (weak, nonatomic) IBOutlet iCarousel *carousel;
#end
ActivityView.m
- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
if (view == nil)
{
_card = (ActivityCard*) [[[NSBundle mainBundle] loadNibNamed:#"ActivityCard" owner:nil options:nil] objectAtIndex:0];
_card.frame = CGRectMake(0, 0, 280.0, 350.0);
_card.layer.cornerRadius = 10.0f;
view = _card;
}
return view;
}
HomeViewController.h
#interface HomeViewController : UIViewController
#property (strong) ActivityView *viewActivity;
#end
HomeViewController.m
_viewActivity = (ActivityView*) [[[NSBundle mainBundle] loadNibNamed:#"ActivityView" owner:nil options:nil] objectAtIndex:0];
_viewActivity.frame = CGRectMake(0.0f, 0.0f, _viewContainer.frame.size.width, _viewContainer.frame.size.height);
[_viewActivity updateActivityUI];
for (UIView *child in _viewContainer.subviews) {
[child removeFromSuperview];
}
[_viewContainer addSubview:_viewActivity];
[_viewActivity.card.enterButton addTarget:self action:#selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];
[_viewActivity.card.lblActivityCount setText:#"11"];
This code has been written when we need to load the ActivityView on a button click.
Have many ways to do it, i will show you a way which i think it's the easiest way.
Create a property of Enter Button and property for all your textfields in ActivityCard.h
In HomeViewController add action for your Enter Button by using my code below.
[self.activityView.cardView enterButton addTarget:self action:#selector(enterButtonDidTouch) forControlEvents:UIControlEventTouchUpInside];
Create a method in HomeViewController.m name enterButtonDidTouch
- (void)enterButtonDidTouch {
NSString *textOfTextField1 = self.activityView.activityCard.textField1.text;
// With the same way you can get others text of text fields and use them
}
I am trying to hide two buttons when I press one button on the ViewController. I can only seem to hide the button I click but using [sender setHidden:YES];. The buttons I have:
- (IBAction)Button1:(UIButton*)sender;
- (IBAction)Button2:(UIButton*)sender;
I can't seem to use Button2.hidden = YES in Button1. Is there a way round this?
You should create button property in your viewController, and use it to hide button:
#interface ViewController ()
#property (nonatomic, strong) IBOutlet UIButton* button1;
#property (nonatomic, strong) IBOutlet UIButton* button2;
- (IBAction)action1:(id)sender;
- (IBAction)action2:(id)sender;
#end
In the actions:
- (void)setButtonsHidden:(BOOL)hidden
{
_button1.hidden = hidden;
_button2.hidden = hidden;
}
#pragma mark Actions
- (IBAction)action1:(id)sender
{
[self setButtonsHidden:YES];
}
- (IBAction)action2:(id)sender
{
[self setButtonsHidden:YES];
}
Also you can use NSArray to store all buttons references:
#interface ViewController ()
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray* allButtons;
<.....>
In setButtonsHidden: method:
- (void)setButtonsHidden:(BOOL)hidden
{
[_allButtons enumerateObjectsUsingBlock:^(UIButton* obj, NSUInteger idx, BOOL *stop) {
obj.hidden = hidden;
}];
}
Viewcontroller.h
#property (weak, nonatomic) IBOutlet UIButton *button1;
#property (weak, nonatomic) IBOutlet UIButton *button2;
- (IBAction)button1:(UIButton *)sender;
- (IBAction)button2:(UIButton *)sender;
Viewcontroller.m
- (IBAction)button1:(UIButton *)sender
{
_button1.hidden = YES;
_button2.hidden = YES;
}
- (IBAction)button2:(UIButton *)sender
{
_button1.hidden = YES;
_button2.hidden = YES;
}
I'd appreciate if anyone could help.
I've created a slider and a label that changes it's text value when the user changes the slider. It's not working, for some reason.
Thoughts ?
I'm using xcode 6.1
Here's the code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *numberField;
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderLabel.text = #"50";
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender {
[self.nameField resignFirstResponder];
[self.numberField resignFirstResponder];
}
- (IBAction)sliderChanged:(UISlider *)sender {
int progress = lroundf(sender.value);
self.sliderLabel.text = [NSString stringWithFormat:#"%d", progress];
}
#end
take UISlider outlet and add selector as value changed in viewDidLoad like below.
IBOutlet UISlider *slider;
and to add selector in viewDidLoad
[slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
I'm trying to change a specific CollectionViewCell once it's tapped on. I created a custom cell:
projectCell.h:
#import <UIKit/UIKit.h>
#interface ProjectCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *projectLabel;
#property (weak, nonatomic) IBOutlet UILabel *projectCount;
#property (weak, nonatomic) IBOutlet UITextField *projectTextField;
#end
projectCell.m:
#import "ProjectCell.h"
#import "QuartzCore/CALayer.h"
#implementation ProjectCell
{
BOOL editMode;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)awakeFromNib {
// Custom initial code
}
-(void)editProject {
if (editMode == YES) {
// disable editMode and update the project cell
editMode = NO;
_projectTextField.hidden = YES;
} else if (editMode == NO) {
// Enable editMode and update the project cell
editMode = YES;
_projectTextField.hidden = NO;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And I'm trying to access it like this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
// Get the tapped cell
ProjectCell *projectCell = (ProjectCell *)[collectionView cellForItemAtIndexPath:indexPath];
// Run method
[projectCell editProject];
}
But I get the error while building:
No visible #interface for 'ProjectCell declares the selector 'editProject'
What am I doing wrong here?
Thanks a lot for help!
I think you need to declare your function in public space (in the header).
#import <UIKit/UIKit.h>
#interface ProjectCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UILabel *projectLabel;
#property (weak, nonatomic) IBOutlet UILabel *projectCount;
#property (weak, nonatomic) IBOutlet UITextField *projectTextField;
-(void)editProject;
#end
When I try to change the text of a label on button click, the code compiles but it doesn't work. I click the button and nothing happens.
Currently, im just trying to display "Error" on label x1Label when button solveButton will be pressed. Could you please help me fixing it?
.h
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize errorLabel;
#synthesize aField, bField, cField;
#synthesize x1Label, x2Label;
#synthesize solveButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aField.delegate = self;
bField.delegate = self;
cField.delegate = self;
aField.keyboardType = UIKeyboardTypeNumberPad;
bField.keyboardType = UIKeyboardTypeNumberPad;
cField.keyboardType = UIKeyboardTypeNumberPad;
NSString *str = #"car";
errorLabel.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
}
- (IBAction)solveButton:(id)sender
{
errorLabel.text = #"Error";
}
#end
.m
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *aField, *bField, *cField;
}
#property (weak, nonatomic) IBOutlet UILabel *errorLabel;
#property (strong, nonatomic) IBOutlet UITextField *aField;
#property (strong, nonatomic) IBOutlet UITextField *bField;
#property (strong, nonatomic) IBOutlet UITextField *cField;
#property (weak, nonatomic) IBOutlet UILabel *x1Label;
#property (weak, nonatomic) IBOutlet UILabel *x2Label;
#property (weak, nonatomic) IBOutlet UIButton *solveButton;
#end
Thank you
You are writing on errorLabel not on x1Label.
Did you connect the SolveButton action to the button in your storyboard?
if not go to storyboard > click on your button > go to Connection Inspector (on the right panel) and link the action to you button.