I have a view: "myView" , 3 buttons in this view with title:"myButton1","myButton2" and 3... and a property in myView, NSString: "myString". How can i do this: When I press this button I want to change myString in buttons title property("button1..2,3").
very simple
-(void)viewDidLoad{
[super viewDidLoad];
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
[btn2 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
[btn3 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)buttonTapped:(UIButton*)sender{
[sender setTitle:myString forState:UIControlStateNormal];
}
I assume that myView is either the view of a UIViewController (i.e. accessible through myViewController.view) or it is one of its subviews / subsubview etc.
If you are using Interface Builder to create your view controller (e.g. in a storyboard) you can open the assistant editor
(click the icon in the upper right corner)
and drag a line from each button to your viewController.h file (hold CTRL and drag to the source code in the lower window).
Select 'Action' as connection type and give each method a unique name like buttoniTapped. Open the viewController.c file in the editor. There you should find the newly created action methods. Now make those methods change your string:
- (IBAction)button1Tapped:(id)sender {
self.myView.myString = #"Button 1 was tapped.";
}
- (IBAction)button2Tapped:(id)sender {
self.myView.myString = #"Button 2 was tapped.";
}
- (IBAction)button3Tapped:(id)sender {
self.myView.myString = #"Button 3 was tapped.";
}
(This will only work if you create a property myView in myViewController for the view that contains the three buttons.)
Generally I think it is a better idea to make myString a property of the view controller rather than the view because it makes things easier and separates your data from your view.
Related
This code worked in a UIView container, but after changing that container to a UIViewController, the code no longer works:
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
// set button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60, 30, 220, 220);
[button setImage:self.obj.img forState:UIControlStateNormal];
[button addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
[button setUserInteractionEnabled:YES];
[self.view addSubview:button];
}
-(void) selected:(UIButton *) button // also tried (id) sender
{
NSLog(#“Button Selected“);
}
These containers are created programmatically (dynamically), from web services, so I cannot use Storyboard. The reason for changing to a UIViewController is that I need a segue to another screen. Also, these containers are placed in a UIScrollView.
Everything else is working properly, except the selector.
Setting the userInteraction wasn't necessary earlier, but I've gone ahead and tried that in different configurations. I've added this to the scrollview, the viewController, and the button, just for good measure.
I even tried some online tutorials for programmatically creating UIButtons in UIViewControllers and was able to do that just fine. Not sure what the issue is here. Thanks.
I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
I have a button:
- (UIButton *)subwayABBtn
{
if (!_subwayABBtn)
{
_subwayABBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_subwayABBtn.tag = ADRESS_SUBWAY_BTN_TAG;
[_subwayABBtn setTitle:#"Метро" forState:UIControlStateNormal];
[_subwayABBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.contentView addSubview:_subwayABBtn];
}
return _subwayABBtn;
}
How can I change the title of the button with a tag from another class? I need to change the title of the button.
You would need a reference to the other class in your view controller, declare the button in its header file and use the method -viewWithTage:
Example:
OtherClass *otherClass; // This should be set from previous class and a property in your current class
UIButton *buttonFromOtherClass = [otherClass viewWithTag:BUTTON_TAG];
[buttonFromOtherClass setTitle:#"Метро" forState:UIControlStateNormal];
I didnt get what you meant by another class. If its from your view where button is added, then you can use the below code to get UIButton
UIButton *btn = (UIButton *)[self.contentView viewWithTag:ADRESS_SUBWAY_BTN_TAG];
[btn setTitle:#"New title" forState:UIControlStateNormal];
use delegates and #protocol to access parent view controller in ios
refer tis simple example
link1
I have a coreplot project. The class of the view in viewcontroller has been changed to CPTGraphHostingView.
After creating the graph (this works fine),
I am adding a button as a subview , this launches a popover
the button shown is inverted. I have the same control in different view controllers and it works fine. The Popover also appears fine.
Here is the code that creates and adds the UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(monthSelection)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Change Month" forState:UIControlStateNormal];
button.frame = CGRectMake(330, 650, 111, 40);
[self.view addSubview:button];
[here is a screenshot of whats happening. UIButton is suppose to show somewhere below the popover. What am i missing.
Do not add subviews to the graph hosting view. Instead, make other views siblings of the hosting view, i.e., add them as subviews of the same superview.
i want to add some text fields and the buttons to the popover view ...
how to add those and i hav to read the text added into text field by user later ...
....should i need seperate view controller class to control popover view or can i control with existed one which calls popover view....
any help Appreciated..
Create a UIViewController using IB or programatically. Add needed buttons, textfields and maybe some logic there and use this viewController for initializing an UIPopoverController (as Gendolkari suggested).
The UIPopoverView is created by passing in a ViewController which controls the content of your popover. Check out the initWithContentViewController:, this allows you to pass your own ViewController. That ViewController would have a view with the text fields and button that you want.
Try using this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.tableView addSubview:button];