I have created a popup and in that there is a list of content are displaying in a table view. Now when I click into any row of the tableview, it should call a method, which is available in the parent view not in the popup view. If I use any button in that popup then after selecting a row if I click into that button then it works. The button action is mention in parent view in this way.
[controller.gotoButton addTarget:self action:#selector(clickMe:) forControlEvents:UIControlEventTouchUpInside];
So how to call a method when clicking into a table rows?
Here you can find a pretty good description of delegates.
What you would like to do, can be solved with a protocol/delegate. You should create the protocol in the Popup view's header file, and the implementation in the *.m class. Your parent view should implement the protocol, and don't forget the connection line, the myPopupObject.delegate = self; + the implementation of the protocol's method.
I suggest to use the
if ([delegate respondsToSelector:#selector(myMethod:)]) {
//call the selector
}
verification in the Popup view class, because if the protocol's method is optional, and you hadn't implemented it in the parent class, your app will crash (you won't receive any error/warning message from the compiler, because it was an optional method).
in rowDidSelect,
create a object for the parentview
eg:
parentClass *pc=[parentClass alloc]init];
[pc method:];
Related
can anyone say how do i observe for action in Reactive Cocoa for a UIButton or UIControl..
An alternative way is to bind the view to the view model.And observe changes on the Mutable Property.
I tried using below code but none is firing.
self.rollBtn.reactive.trigger(for: .touchUpInside).observeValues {
value in
print(value)
}
EDIT: Actually i am trying to get the sender on button Tap..how can i do that?
You have done nothing wrong in this code snippet - trigger(for:) is one of the ways to get notified in RAC 5.0. It should print () for every press on the button.
Have you linked the button with the view, if you are using Storyboard or Interface Builder? Where did you place this piece of code? Make sure you place it in viewDidLoad or awakeFromNib so that it gets called before the view is presented.
-
EDIT: Actually i am trying to get the sender on button Tap..how can i do that?
As mentioned in the comments, trigger(for:) returns a Signal<(), NoError>. It doesn't include the sender with the value event. You would need to reference the sender manually, e.g.:
button.reactive
.trigger(for: .touchUpInside)
.observeValues { [unowned button] in
_ = button
}
I am designing apple watch application where i need to show top10 feed title and i have successfully shown it. in next step i have to add action event to tap which will redirect user to next screen but i am confused which controller to use here. i have to show all feeds in pagination format and then on click i have to show its detail view.
does anyone tried with this approach? i am using UIButton over there but its having text limitation so cant use it and for tableview it scroll verticaly where as i need horizantle scroll.
alph0x's answer is pretty useful. But you can also do another thing to perform what you are asking in case you only want that the action will do when push in a specific button of the row.
This second solution consists on create a class for the custom row with an IBAction
#import <WatchKit/WatchKit.h>
#interface MyRow : NSObject
// Methods
- (IBAction)buttonClick;
#end
And in the buttonClick method, you can specify the action as in the follow example using pushControllerWithName:context to go to a specific interface controller
#import "MyRow.h"
#implementation MyRow
- (IBAction)buttonClick {
[self goToInterface:#"feedInterface"];
}
- (void)goToInterface:(NSString *)interfaceName{
NSDictionary *contextToSend = [NSDictionary dictionaryWithObjectsAndKeys:#"FeedTitle", #"title",
#"lalala", #"secondValue",
#"lelele", #"thirdValue",
#"other value", #"other", nil];
[self pushControllerWithName:interfaceName context:contextToSend];
}
#end
You can send your row info through context. In that example I have decided to send a dictionary with some values.
In the interfaceName param you have to specify the Interface Controller Identifier that you can set in your storyboard. See the image below:
And tell to the XCode that your table row has the custom class MyRow
Note: don't forget to assign your button to the IBAction method ;)
If I understand, you need to have a table with 10 rows, and you need to be able to tap in one row, and view the details about the one you selected, if thats correct, you only need to use the Method from WKInterfaceTable "- (id)rowControllerAtIndex:(NSInteger)index", with this one (works like UITableView one with the delegate) you can handle every row action after being tapped.
I have an iPad app using XCode 4.6, Storyboards, iOS 6.2. I looked at SO and Google, but nothing about when to send the message to the button.
I am trying to save some data to CoreData when the user leaves a "scene" but it's not working. Here is the code:
-(void) viewWillDisappear:(BOOL)animated {
[bSavePreferences sendActionsForControlEvents:UIControlEventTouchUpInside];
}
-bSavePreferences is in the same class as viewWillDisappear, but it never gets called. Why?
UPDATE: I should have mentioned that I am using a UITabBarController to control which page is selected.
A few things...
You shouldn't be invoking an action this way. You should just call the IBAction method directly to do the saving.
You need to call [super viewWillDisappear:animated] at some point when implementing viewWillDisappear.
As for the root problem, viewWillDisappear is called in response to the view being removed from the view hierarchy. It's possible that the UIButton would no longer be able to receive events at this point. I encourage you to look at the documentation.
There are multiple methods:
One way is to add a target action to your button:
[myButton addTarget:self
action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
The second option is to use IBActions:
- (IBAction)ButtonPressed { NSLog#"press"; }
Make sure you wire your outlets correctly!
I am building an interface, where I can add events like in a calendar.
In the AddAEventViewController I have Buttons to set the starttime, duration and recurrence.
Every time you press a button a viewcontroller comes up with a UIDatePicker, where you can set your time. The picked component is than displayed in a UITextField. Now when I press the Done-Button, it dismisses the ModalViewController and I am back to my AddAEventViewController. Next to the Durationbutton e.g. is a UILabel, where I want to show now the just picked and in the textfield shown duration.
How do I get access to the AddEventViewController out of an other ViewController? I tried to alloc and init a new one there, but it didnt work!
- (IBAction)pressedDoneButton:(id)sender {
_mainAddWishViewController.labelDuration.text=textFieldDuration.text;
[self dismissModalViewControllerAnimated:YES];
}
Can someone help me please!
Thank you Jules
There are several ways you can do this, all of them documented here. Reading and understanding them will help you a lot in iOS software development.
There are many ways to achieve this. Here is one that is fairly straightforward.
In the "child" viewController, add a delegate property and set it to the parent view controller.
Then in your Done button handler, do something like:
[self.delegate performSelector:#selector(didComplete) withObject:self]
In the parent view controller, define a method as follows:
- (void) didComplete: (YourSubViewControllerClass *) sender
{
self.labelDuration.text = sender.textFieldDuration.text
}
Basically, this implements an informal protocol whereby the subViewController informs the main view controller that it is finished and input values are available.
Note that if you cancel out of the subViewController, don't send the didComplete message.
I'm playing around with drawing in iOS apps. I have a class that is a subclass of UIView that draws some lines and stuff. When the user presses a button, I instantiate the class and do an addSubView on the view of the main UIViewController of the app. The more times the user presses that button, the more instances of that class get added to the view. It's working just fine.
Now I want to provide the user a way to delete one of those views. So far I've put a [self removeViewFromSuperview] into the touchesBegan method of the custom UIView. So when the user presses the drawing it gets removed from the view. But, it's not actually deleted, right? Since the view was instantiated within the method that executes when the button is pressed I have no way to reference it from within the UIViewController. What's the appropriate way to make sure I'm not wasting memory with a UIView that was created and removed?
On a related note, if I was to put a toggle switch on the main window's UIView that toggles delete, how can I check from within touchesBegan if that toggle switch is set to delete=yes? Would I have a some sort of boolean variable in the AppDelegate that I can check from within the UIView subclass? How would I reference that?
Thank you for your help,
Stateful
If you add the view like this:
UIView *viewBeingAdded = [[[UIView alloc] init] autorelease];
[view addSubview:viewBeingAdded];
You can remove it without leaking memory:
[theViewAboutToBeRemoved removeFromSuperview];
Regarding the UISwitch, you don't need to keep its value anywhere unless you need it for something else. You can access its value directly:
if ([theSwitch isOn]) { ... }
You don't even need an IBOutlet, you can access the switch with its tag:
UISwitch *theSwitch = (UISwitch *)[view viewWithTag:<# switch tag number #>];
if ([theSwitch isOn]) { ... }
In this case you must set a unique tag number for the switch in Interface Builder or when you create it.
When you do [mainView addSubView:myView], mainView will retain myView. If you created myView with alloc/init, then you retained it also. If you don't need myView after adding it to the main view then simply do [myView release] after adding it. When you remove it from the main view, it will get released and deallocated.
If you create the UIView with alloc/init, add it to the superview then release the view, the superview will retain it. When it is removed with removeViewFromSuperview it will be dealloc'ed.
I typically autorelease a view after adding it, leaving the parent the only reference.
As to checking a toggle, you could add an IBOutlet so you can inspect it directly. (This may not be pure MVC, but I don't know if putting it in [UIApplication sharedApplication].delegate is necessarily cleaner.)