UIButton in UIScrollview inside UItableViewCell handle event with data - ios

I have an CustomUIScrollView that have many CustomUIButton inside (each button is a news item that have an image and title --> each button have a news ID
When touch inside button, the touchupInside event fire --> everything is OK
But i want when touch in button, a detail news Page, detailViewController, will create and push into navigationController (like BBC NEWS app in Store) and the detail Page will load webpage has ID equal with ID of Button we has touched.
My tableviewController is rootViewController, how can I pass event with ID data from Button in Scrollview inside UITableViewCell to my rootviewController to push a new DetailViewController
Thank in advance. (See picture make more easy to understand)

enter code hereHi i used this code try this..
in this code i pass button tag form calling method and compare to called method and then take your action whatever you need.
Thank You
-(IBAction)btnpress:(id)sender
{
UIButton *btn = (UIButton *)sender;
NSLog(#"%d",btn.tag);
if(img_alphabet.tag == btn.tag)
{
}
}

Related

Copying UIAlertController button behaviour

I am creating custom dialogs for my app and some what copying UIAlertController in some aspects. How should I implement the behaviour where when you click any action from alert/dialog the controller is dismissed.
How does Apple do it without making us manually specify for each action handler that it should dismiss the view controller?
I have like them one view controller class:
#interface MyAlertViewController : UIViewController
- (void)addAction:(MyAlertAction *) action;
//...
And one class for the actions:
#interface MyAlertAction : NSObject
- (instancetype)initWithTitle:(nullable NSString *)title handler:(void (^)(MyAlertAction *action))handler;
EDIT: How I did it taking in accord the answer feedback:
//MYAlertViewController.m
- (void)viewDidLoad {
for (int i = 0; i < self.actions.count; i++) {
MYAlertAction *action = self.actions[i];
button = [[UIButton alloc] initWithFrame:CGRectZero];
button.tag = i;//this here is how I link the button to the action
[button addTarget:self action:#selector(executeAction:) forControlEvents:UIControlEventTouchUpInside];
[actionStackView addArrangedSubview:button];
[self.actionsStackView addArrangedSubview:actionLayout];
}
}
- (void)executeAction:(UIButton *) sender{
[self dismissViewControllerAnimated:YES completion:^{
//this is where the button tag comes in handy
MYAlertAction *actionToExecute = self.actions[sender.tag];
actionToExecute.actionHandler();
}];
}
How does Apple do it without making us manually specify for each action handler that it should dismiss the view controller?
You are confusing two different things:
The UIAlertAction's last initialization parameter, the handler parameter, which you get to set from outside, and which is to run after the button is tapped and after the alert has been dismissed. It is a block.
The actual button's action, which the client can't set or see. It is configured by the alert controller. It is a selector.
So now, you play the role of the UIAlertController. In your
- (instancetype)initWithTitle:(nullable NSString *)title handler:(void (^)(MyAlertAction *action))handler;
the client hands you the first action I mentioned, the block, and you store it for later execution. But the second action, the button action, the selector, is entirely up to you as you create the button in response to this call.
So as you configure the button, just configure it with a target/action pair that calls into a method of your view controller, just as for any button. In method, when called, the view controller dismisses itself, and in the completion handler of the dismissal, calls the block.

Perform a click to other buttons objective c

So how can i make a button click other buttons in objective c for iOS? I use xcode.
IBOutlet UIBarButtonItem *save1;
IBOutlet UIBarButtonItem *save2;
IBOutlet UIBarButtonItem *save3;
IBOutlet UIBarButtonItem *save4;
These are the buttons, I want that button "save4" click button "save1", "save2" & "save3".
I understand the actions and outlets and I have that all connected, but I just wan't the code to click other buttons, the problem I have is that those buttons are on another view and I tried many code but they don't work. Here is a download link: Download
I think you are confused between "Outlets" and "Actions". For handling button clicks, you must define "Actions" for your buttons. For eg, write the following code in your view controllers .m file:
- (IBAction) saveOnePressed:(id)sender{
// Button one is pressed. Do your actions here.
}
And connect your button to this action.
EDIT:
As per rmaddys suggestion:
You can define the Action for your fourth button as the following:
- (IBAction)saveFourPressed:(id)sender{
// Here you can call all the other 3 actions
[self saveOnePressed:nil];
[self saveTwoPressed:nil];
//......
}

Prepopulating a UITextField with Core Data and NSFetchedResultsControllers

I have a simple application and I require a bit of assistance with one new feature.
The application is a table view (TV1) which is populated by the user adding an entry (a button in the navigation bar which modally brings up a new view controller for the user filling in a name, event and amount text field and selecting a date from the date picker).
The table view controller is part of a tab bar controller where the first tab shows everything (timeline) and the second tab shows just the list of events. If you click on an event, you segue over to see all transactions with that event.
I've added a bar button item here as well which segues to the Add Entry view controller, with the aim of having the event already selected.
What I mean by this is:
Say you have 3 events called Wedding, Birthday and Anniversary in your transactions. If you click on the Event tab, you'll see Wedding, Birthday and Anniversary in a Table view. Clicking Birthday will show you all the transactions where event = Birthday.
I have a bar button item which will allow me to add entries from this screen, but because it's coming from the Birthday event, I want that event text field to already be filled in, in the Add Entry with the name of this event (Birthday in this case).
I'm using Core Data and NSFetchedResultsController.
The situation right now is, when I press the button to add a new entry from the Selected Event (Birthday), it's not pre-configuring the event text field with Birthday in the Add Entry screen.
Here's some code which should hopefully explain what I'm doing.
From the Event Tab
// This segues over to the selected event (Birthday).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.eventTableView indexPathForCell:sender];
Occasion *selectedOccasion = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([segue.identifier isEqualToString:#"Selected Occasion Segue"])
{
ESelectedOccasionTableViewController *selectedOccasionTVC = [segue destinationViewController];
[selectedOccasionTVC setOccasion:selectedOccasion];
}
}
From the Selected Event Tab (Birthday), I create two segues, one for editing a cell and one for creating a new entry:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.selectedOccasionTableView indexPathForCell:sender];
Transaction *seletedTransaction = [self.fetchedResultsController objectAtIndexPath:indexPath];
// The first segue is to "edit" the existing entry
if ([segue.identifier isEqualToString:#"Editable Cell"])
{
EDetailViewController *dvc = (EDetailViewController *)segue.destinationViewController;
dvc.selectedTransaction = seletedTransaction;
}
// This segue is to create a new entry from the event
else if ([segue.identifier isEqualToString:#"Create New Entry From Event"])
{
EddEntryViewController *eAppEntryViewController = (EAddEntryViewController *)segue.destinationViewController;
[eAppEntryViewController setSelectedTransaction:selectedTransaction];
}
}
The first segue is for editing the cell (which works perfectly), but in the second segue, the one I'm interested in here, I'm not actually clicking on a cell; I'm clicking on a bar button item which will modally bring up the add entry. I want to take just the "event" (Birthday) from this Table View and pre-fill the "event text field" in the Add Entry.
The setSelectedTransaction in the Add Entry is:
- (void)setSelectedTransaction:(Transaction *)selectedTransaction
{
_selectedTransaction = selectedTransaction;
self.occasionTextField.text = _selectedTransaction.occasion.title;
}
I can't imagine I'm very far from getting this working, but any assistance on this would really be great because I'm really stuck.
Should I be setting something in viewDidLoad? (That did not work either).
I'm not selecting a table view cell which is why I understand that the [eAppEntryViewController setSelectedTransaction:selectedTransaction]; line isn't actually passing anything, because I'm clicking on a bar button item, not a table view cell.
The model is: a Transaction Entity that links to the Date and Event Entities.
I have resolved this issue in the following way:
EAddEntryViewController *eAppEntryViewController = (EAddEntryViewController *)segue.destinationViewController;
[eAppEntryViewController setSelectedEvent:self.occasion.title];
Which called:
- (void)setSelectedEvent:(NSString *)selectedEvent
{
_selectedEvent = selectedEvent;
}
In the viewWillAppear, I did:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.occasionTextField.text = self.selectedEvent;
}
This works the way it is and I'm happy with this.
So I might not be understanding the question fully. But you can add a "Touch Up Inside" event to the bar button item and then pull the text from the "sender" object. Then, either store that text as an NSString property in the interface for later use, or set your desired textbox straight from the bar button item action.

iOS: show same "Please sign-in" screen for some view controllers, then change it to real content?

I have an app with tabbed view: search | subscribes | messages
All tab buttons are visible at app start even for unauthorized user.
But when unauthorized user clicks on let's say messages tab, I want to show "MessagesViewController" but it must show grey screen:
Please sign-in. [button sign-in] [button registration]
When user clicks [button sign-in] - modal UIViewController appears. After sign-in user goes back to "MessagesViewController" tab, but this time user can see his messages. Same grey screen must be in subscribes tab.
I'm new to iOS so I want to know what is a proper way to do this. Subscribes and messages are table views. Do I need to place another view on top of tables to overlap them and then hide it after authorization? Or can I create one separate "unauthorized" controller, connect it with tabs and reuse it? If so, how can I connect messages and subscribes controllers back to tabs after sign in?
Create a baseViewController, make all three viewController's extend that base.
Think of a method that will show the user "please sign-in" message. A gray UIView (with a UILabel and two UIButtons) overriding all the content seems good to me.
Add the buttons on that gray UIView, with self s as targets
Check in the viewWillAppear method whether the user is not logged in or not. Show the gray view if not.
Create a boolean and before you fire the segue you proof, if the boolean is yes or no. When the boolean is yes, the segue is performed. Otherwise a segue to your Login View is performed.
I hope this solves your problem
you can use
- (void) viewWillAppear:(BOOL)animated
to do this
#interface Messages
{
BOOL loggedIn;
UILabel *pleaseSignIn;
UIButton *signin;
UIButton *register;
}
- (void) viewWillAppear:(BOOL)animated
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:#"logged"] isEqualToString:#"YES"])
{
loggedIn = YES;
}
else
{
loggedIn = NO;
}
if(loggedIn)
{
//Display messages
pleaseSignin.hidden = YES;
signin.hidden = YES;
register.hidden = YES;
}
else
{
//Remove messages view
pleaseSignin.hidden = NO;
signin.hidden = NO;
register.hidden = NO;
}
}

iOS: setting buttons pressed state

I am trying to find the best approach to doing this. I have 5 custom buttons on a view controller and I am trying to have the button stay highlighted if it is clicked. I know how to do this but I am trying to only allow 1 button to be highlighted at a time. So if a user clicks a button and highlights it, but clicks another, then the most recent button clicked will stay highlighted and the previous will unhighlight. What would be the best way to accomplish this?
You should keep a reference to all your buttons (for example, if you use IB, have links in your code like #property (nonatomic, strong) IBOutlet UIButton *button1; for all your buttons).
Then link all your buttons to the same method for a press on the button. I'll call it buttonPressed.
Impement it like this :
- (IBAction)buttonPressed:(id)sender {
UIButton *buttonPressed = (UIButton*)sender;
NSArray *buttons = [NSArray arrayWithObjects:_button1, _button2, _button3, nil];
bool buttonIsHighlighted = NO;
// Check if a button is already highlighted
for (UIButton *button in buttons) {
if (button.highlighted) {
buttonIsHighlighted = YES;
}
}
// If a button is highlighted, un-highlight all except the one pressed
// If no button is highlighted, just highlight the right one
if (buttonIsHighlighted) {
for (UIButton *button in buttons) {
if (buttonPressed == button) {
buttonIsHighlighted = YES;
} else {
button.highlighted = NO;
}
}
} else {
buttonPressed.highlighted = YES;
}
}
I can't test this code but I'm pretty sure it should work. Let me know if something's wrong.
Solution 1:
Put your buttons in an NSArray and when user clicks on a button check if another is highlighted. If YES, unhighlight it and highlight the one was pressed. If NO, highlight directly the one pressed.
Solution 2:
You can save the highlighted button in a global variable declared in #interface or in a #property. When users click the new one unhighlight the previous.

Resources