ActionSheet in NavController with TabBarController - Exc_Bad_Access - objective-c

I'm currently have a weird problem.
I have an actionsheet with pickerView in with two button (cancel, and Ok).
They work perfectly until i choose a value in that picker and i click on the first button of my TabBarController which contain my NavigationController.
My actionsheet is declared in my .h and has property (nonatomic, retain), i release it in my dealloc function.
I have an exc_bad_access if i release this object and i dont know why because i allocated it.
This is my function which create my Actionsheet if is nil, and build it.
- (IBAction)select_marque :(id)sender
{
if (!actionSheet_marque)
{
actionSheet_marque = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
}
[actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
picker_marque = [[UIPickerView alloc] initWithFrame: pickerFrame];
picker_marque.showsSelectionIndicator = YES;
picker_marque.dataSource = self;
picker_marque.delegate = self;
[actionSheet_marque addSubview: picker_marque];
[picker_marque release];
UILabel *lbl = [[UILabel alloc] initWithFrame: CGRectMake(110, 7.0f, 150.0f, 30.0f)];
lbl.text = #"Marque";
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
[actionSheet_marque addSubview:lbl];
[lbl release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Ok"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:closeButton];
[closeButton release];
UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Cancel"]];
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(5, 7.0f, 50.0f, 30.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
cancelButton.tintColor = [UIColor redColor];
cancelButton.tag = 1;
[cancelButton addTarget:self action:#selector(cancelActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet_marque addSubview:cancelButton];
[cancelButton release];
[actionSheet_marque showInView: self.navigationController.view];
[actionSheet_marque setBounds:CGRectMake(0, 0, 320, 485)];
}
I check also if the pointer is nil in dealloc method....
Thanks

If you have property of UIActionsheet then you should use self.actionSheet_marque when you allocate it. Try changing code like this
if (!actionSheet_marque)
{
self.actionSheet_marque = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease];
}
[self.actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
And in whole function except this line if (!actionSheet_marque)

Related

Jssocials not working in UIWebView/WKWebView

I want to use jssocial plugin in iOS,there is no response when user clicks on buttons be it a Facebook, twitter or Instagram from UIWebView and also not from WKWebView so, if anybody have implemented it in iOS then please guide me about it.
Any help will be appreciable. Thank You
Set WKWebView properties and delegates as follow
WKPreferences *prefs = [WKPreferences new];
prefs.javaScriptEnabled = YES;
prefs.javaScriptCanOpenWindowsAutomatically = YES;
WKWebViewConfiguration *theConfig = [[WKWebViewConfiguration alloc] init];
theConfig.preferences = prefs;
webView = [[EasyJSWKWebView alloc] initWithFrame:self.view.bounds
configuration:theConfig
withJavascriptInterfaces:interfaces];
webView.UIDelegate = self;
webView.navigationDelegate = self;
[webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
if (#available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[webView loadRequest:requestObj];
[self.view addSubview:webView];
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures: (WKWindowFeatures *)windowFeatures
{
ViewPopUp = [[UIView alloc] initWithFrame:self.view.frame];
[ViewPopUp setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:ViewPopUp];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
[btnBack setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[ViewPopUp addSubview:btnBack];
PopUpView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) configuration:configuration];
PopUpView.allowsBackForwardNavigationGestures = YES;
PopUpView.navigationDelegate = self;
[self.view addSubview:PopUpView];
return PopUpView;
}

set font size to UIActionSheet title

I need to set size UIActionSheet title "Select option to Copy"
with this code :
[[UIActionSheet alloc] initWithTitle:#"Select option to copy:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"copy all ",nil];
You could take advantage of Runtime Properties to set the font. By using NSAttributedString you can achieve this.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"copy all ",nil];
//Creating Attributed String with System Font size of 30.0f
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"Select option to copy:" attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:30.0f]}];
//Accessing the Alert View Controller property from Action Sheet
UIAlertController *alertController = [actionSheet valueForKey:#"_alertController"];
//Setting Attributed Title property of Alert View Controller
[alertController setValue:attrString forKey:#"_attributedTitle"];
//Displaying the Action Sheet
[actionSheet showInView:self.view];
You can change the title font of uiactionsheet by using this delgate
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
l.text = [(UILabel *)_currentView text];
[l setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
l.textColor = [UIColor darkGrayColor];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
[l setCenter:CGPointMake(actionSheet.center.x, 25)];
[l setFrame:CGRectIntegral(l.frame)];
[actionSheet addSubview:l];
_currentView.hidden = YES;
break;
}
}
}

UIButton addTarget method...not working?

I have a class variable that's an instance of UIActionSheet, called actionSheet. I add an instance of UIPickerView as well as an instance of UISegmentedControl. Though when I tap the "Done" button (instance of UISegmentedControl), my class method dismissActionSheet is never called. Can anyone help me out?
//########################################
-(IBAction)chooseTopicButtonPressed{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = singlePicker.dataSource;
pickerView.delegate = singlePicker.delegate;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(10, 6, 300, 30);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventTouchUpInside];
//dismissActionSheet NOT BEING CALLED FOR SOME REASON!!!
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissActionSheet{
NSLog(#"dismissActionSheet called");
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
The method dismissActionSheet doesn't have any arguments so the #selector should be as follows:
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventTouchUpInside];

How do I remove or stop a button in table view from working after registered amount of taps

I have a button in a table view that adds rows. After a maximum of five rows I want to stop the user adding anymore. Currently I show an alert after button recives 5 taps.
How can I stop the user from using the button past this point? Setting to hidden wont work as its a custom subclass and property hidden is not found on the class
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor darkGrayColor];
titleLabel.text = self.distributionBoard.dbRef;
titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
// Add new appliance button to the table view's footer view
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 0, 300.0f, 100.0f)];
footerView.backgroundColor = [UIColor clearColor];
UIButton *newBoardButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
CGRect buttonFrame = newBoardButton.frame;
buttonFrame.origin.x = footerView.frame.size.width - buttonFrame.size.width;
newBoardButton.frame = buttonFrame;
[newBoardButton addTarget:self action:#selector(addCircuitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:newBoardButton];
self.tableView.tableFooterView = footerView;
}
.....
////limit to five appliances
- (void)addCircuitButtonPressed:(id)sender {
LogCmd();
Circuit *circuit = [[ICCircuitManager manager] newCircuit];
circuit.distributionBoard = self.distributionBoard;
circuit.circuitReference = [NSString stringWithFormat:#"%d", [self.circuits count] + 1];
circuit.createdAt = [NSDate date];
circuit.modifiedAt = [NSDate date];
[self.distributionBoard addCircuitsObject:circuit];
[self loadData];
[self.tableView reloadData];
{
m_buttonTouchCount++;
if ( m_buttonTouchCount == 4)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iCertifi"
message:#"Maximum number of appliances reached"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
// m_buttonTouchCount = 0; // reset to 0 here if required.
}
}
}
Where you have AlertView you can type this code to disable button:
[(UIButton *)sender setEnabled:NO];
or to hide button:
[(UIButton *)sender setHidden:YES];

How to dismiss UIActionSheet with close button and avoid crash?

I have read on another post (how to dismiss action sheet) that I can use
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
to dismiss the uiactionsheet with the close button, as defined in:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[closeButton release];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
However, I am not sure where to place this line, assuming this will solve my problem:
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
Also, the app crashes when I click the close button, with error message "PROGRAM RECEIVED ERROR MESSAGE SIGBRT". I assume my two problems are related. Any help out there?
Just implement dismissActionSheet and put your dismiss message there.
The dismissActionSheet method would look something like this:
-(void)dismissActionSheet {
[sheet dismissWithClickedButtonIndex:0 animated:YES];
}