iOS use variable with another m file when button click change it - ios

i need to change urlStr variable when my button clicked from another file. I'm writing my files; I explained everything i think too easy but hard for me please help me.
Thanks a lot.
Request.h file
#import <Foundation/Foundation.h>
#interface Request : NSObject{
}
Request.m file
#import "Request.h"
#import "MyViewController.h"
static NSString * const urlStr = #"http://google.com";
// I WANT TO CHANGE THIS urlStr WHEN storebutton CLICKED
#interface Request () {
BOOL reachable;
}
MyviewController.h file
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController{
}
#end
MyViewController.m file
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)storebutton
{
NSString *urlStr = #"http://yahho.com"; // WHEN MY store button click action
}
#end

Make urlStr as static variable only, then create a static setter method for it inside Request.m and declare it in Request.h as shown below.
+ (void)setURLString:(NSString *) urlString
{
urlStr = urlString;
}
call this method on done button press as
-(void)storebutton
{
NSString *newURL = #"http://yahho.com";
[Request setURLString:newURL];
}

Related

How to add value in NSMutableArray when button is pressed

for my iOS app I want to initiate an NSMutableArray and change the Object the array holds during runtime with buttons. So far I was able to initiate an array in viewDidLoad {} in the ViewController.m but now i can't access it in my buttonPressed method. How can I make the array accessible for the hold file?
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSMutableArray *toCalculate = [#[#0] mutableCopy];
}
- (IBAction)numbersButtonsPressed:(UIButton *)sender {
NSLog(#"%ld\n", sender.tag);
[toCalculate addObject:[NSNumber numberWithLong:sender.tag]];
}
Declare variable as global(class variable) not in local variable(function).
What is global variable?
Solution for the problem is:
#import "ViewController.h"
#interface ViewController (){
NSMutableArray *toCalculate;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
toCalculate = [#[#0] mutableCopy];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)numbersButtonsPressed:(UIButton *)sender {
NSLog(#"%#",toCalculate);
[toCalculate addObject:[NSNumber numberWithLong:sender.tag]];
}
#end
Thanks Paul,
it work like he said if you implement it as a #property in the ViewController.h file.
#interface ViewController: UIViewController
#propety NSMutableArray *giveItAName
#end

iOS SDK - Track viewDidLoad method

I am working on a SDK, where I will have to make a call to our server whenever viewDidLoad method of any UIViewController is called in the app integrated with our SDK. I am trying to use Categories as shown below:
#import "DymmyViewController.h"
#interface DymmyViewController ()
#end
#protocol DummyDelgate;
#interface UIViewController (DummyAddition)
-(void)viewDidLoad;
#end
#implementation DymmyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#end
#implementation UIViewController (DummyAddition)
-(void)viewDidLoad{
//server call
}
#end
Nothing happens with this and I get a warning saying "Category is implementing a method which will also be implemented by its primary class"
I understand this is not the way to get this done. Is there any other way I can do this? May be using NSNotification?
This is the way to use protocols and delegates. Delegated are good techniques to respond from one class to another class.
DummyView.h
#import <UIKit/UIKit.h>
#class DummyView;
#protocol DummyViewDelegate <NSObject>
- (void)addItemViewController;
#end
#interface DummyView : UIViewController
#property (nonatomic, weak) id <DummyViewDelegate> delegate;
#end
DummyView.m
#import "DummyView.h"
#interface DummyView ()
#end
#implementation DummyView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backButtonClicked:(id)sender {
[self.delegate addItemViewController];
}
ViewController.h
#import <UIKit/UIKit.h>
#import "DummyView.h"
#interface ViewController : UIViewController <DummyViewDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
DummyView *acController;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
acController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addItemViewController{
}
Why not just make a tracking view controller available in the SDK. The app dev should then make all of their view controllers of this base type. You might need a couple of base classes (e.g. if you're also using TableViewControllers).
In SDK:
#interface TrackingViewController : UIViewController
#end
#implementation TrackingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Make the call here
// ....
}
In app:
#import "SDK.h"
#interface MyViewController : TrackingViewController ...

Apple Watch - Watch Kit - Delegate method not being called

Ok so I am attempting to pass an int to another interface, edit the int and give it back to the original interface. I am trying to use a delegate to achieve this and I believe I have it setup correctly and it appears the method is not being called when its supposed to.
//
// InterfaceController.h
// DelegateTest WatchKit Extension
//
// Created by Rohan Hodge on 20/10/2015.
// Copyright © 2015 Hodge Development. All rights reserved.
//
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "SecondController.h"
#interface InterfaceController : WKInterfaceController <DelegateTest>
{
NSTimer *Update;
}
#property (strong, nonatomic) IBOutlet WKInterfaceLabel *FirstControllerLabel;
#property (nonatomic,assign) int FirstInteger;
#property (nonatomic,assign) int RecievedInteger;
#property (nonatomic,assign) NSString *PassString;
#end
// InterfaceController.m
// DelegateTest WatchKit Extension
//
// Created by Rohan Hodge on 20/10/2015.
// Copyright © 2015 Hodge Development. All rights reserved.
//
#import "InterfaceController.h"
#interface InterfaceController()
#end
#implementation InterfaceController
#synthesize FirstInteger;
#synthesize RecievedInteger;
#synthesize PassString;
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
}
-(void)UpdateVoid
{
self.FirstControllerLabel.text = [NSString stringWithFormat:#"%i", FirstInteger];
}
- (void)willActivate {
SecondController *interfaceController;
interfaceController.delegate = self;
Update = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(UpdateVoid) userInfo:nil repeats:YES];
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
-(void)DelegateMethod:(int)ReturningInt
{
[self popController];
FirstInteger = ReturningInt;
self.FirstControllerLabel.text = [NSString stringWithFormat:#"%i", FirstInteger];
}
- (IBAction)UpButton {
FirstInteger++;
self.FirstControllerLabel.text = [NSString stringWithFormat:#"%i", FirstInteger];
}
- (IBAction)DownButton {
FirstInteger--;
self.FirstControllerLabel.text = [NSString stringWithFormat:#"%i", FirstInteger];
}
- (IBAction)PassDataButton {
PassString = [NSString stringWithFormat:#"%i", FirstInteger];
[self pushControllerWithName:#"SecondController" context:PassString];
}
#end
//
// SecondController.h
// DelegateTest
//
// Created by Rohan Hodge on 20/10/2015.
// Copyright © 2015 Hodge Development. All rights reserved.
//
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
//This declaration of delegate.
#protocol DelegateTest <NSObject>
-(void) DelegateMethod:(int)ReturningInt;
#end
#interface SecondController : WKInterfaceController
{
id delegate;
}
#property (nonatomic, assign) id <DelegateTest> delegate;
#property (strong, nonatomic) IBOutlet WKInterfaceLabel *SecondLabel;
#property (nonatomic,assign) NSString *RecievedString;
#property (nonatomic, assign) int FirstReceivedInteger;
#end
//
// SecondController.m
// DelegateTest
//
// Created by Rohan Hodge on 20/10/2015.
// Copyright © 2015 Hodge Development. All rights reserved.
//
#import "SecondController.h"
#import "InterfaceController.h"
#interface SecondController ()
#end
#implementation SecondController
#synthesize SecondLabel;
#synthesize FirstReceivedInteger;
#synthesize RecievedString;
#synthesize delegate = _delegate;
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
//This is where I receive the int inside of a string and split it from the string so I can change it
RecievedString = context;
FirstReceivedInteger = [RecievedString intValue];
// Configure interface objects here.
}
- (void)willActivate {
self.SecondLabel.text = [NSString stringWithFormat:#"%i",FirstReceivedInteger];
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (IBAction)UpButton {
FirstReceivedInteger++;
self.SecondLabel.text = [NSString stringWithFormat:#"%i",FirstReceivedInteger];
}
- (IBAction)DownButton {
FirstReceivedInteger--;
self.SecondLabel.text = [NSString stringWithFormat:#"%i",FirstReceivedInteger];
}
//This is a button that is ment to pass back the int.
- (IBAction)ReturnToOriginalInterface:(id)sender{
[self.delegate DelegateMethod:FirstReceivedInteger];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
#end
I'm new to Stack Overflow, Sorry about the messy code formatting.
P.S I use the Arrow in the top left of the interface to return to the original Interface. Also am using Objective-C
Thanks in advance.
You need to set a property or method to change in your controller (that your first controller will change) and you get back the result with a delegate pattern.
You're attempting to do this in a Watch app, yes? I don't know that delegates don't work, but when I did this for my Watch app, I used the context parameter of WKInterfaceController::presentControllerWithName:context:.
context is a NSDictionary of the values you want to pass around. One of those values could be a pointer to the presenting controller.
So, trying to decipher what you're attempting in your app, I believe the proper thing to do is:
In the ORIGINAL WKInterfaceController:
- (IBAction)buttonThatOpensOtherIC
{
NSDictionary *context = #{
#"firstController" : self,
};
[self pushControllerWithName:#"Other IC"
context:context];
}
}
In the OTHER WKInterfaceController:
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
if (context)
{
self.originalInterfaceController = context[#"firstController"];
}
}
//This is the button that calls the delegate method.
- (IBAction)ReturnToOriginalInterface:(id)sender
{
// [self.delegate DelegateMethod:FirstReceivedInteger];
if (self.originalInterfaceController) {
self.originalInterfaceController.firstInteger = self.returningInt;
}
[self popController];
}
*Note the use of awakeWithContext: in the OTHER interface controller.
DISCLAIMER #1: I haven't executed this code, so there may be a typo in it. It is an adaptation for you of working code I do use.
DISCLAIMER #2: I haven't updated my app for WatchOS 2. I doubt this part of things changed, but it is possible.

xcode - execution error with string

I am trying to store and load some text from global variable in x-code. I declare it in main.m, outside of the main function. Then when I want to access it, I use extern. The application crashes after I click the saveButton second time with text in it. It seems like there would be some error rewriting the global labelString string. Can you figure this puzzle out please?
EDIT: Thanks to BKC, I have made some minor changes in the code, however I still get the same error. The code is updated.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (retain, nonatomic) IBOutlet UITextField *field;
- (IBAction)saveButton:(id)sender;
FOUNDATION_EXPORT NSString *labelString;
FOUNDATION_EXPORT NSString *separator;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize field;
NSString *labelString = #"";
NSString *separator = #"|<->|";
- (IBAction)saveButton:(id)sender {
if([field.text length] != 0) // if field isn't blank
{
if([labelString length] == 0) // nothing stored in labelString
{
labelString = field.text;
}
else // if something is already stored in labelString
{
NSString *str = [NSString stringWithFormat:#"%#%#%#", labelString, separator, field.text];
labelString = str;
}
field.text = #"";
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[field release];
[super dealloc];
}
#end
Thank you.
The way you are defining the variable isn't right way. i use any global variable in my project something like this..
Declare the variable in .h file
FOUNDATION_EXPORT NSString *labelString; // Use extern if mixing with C++
Define the varibale in .m file
NSString *labelString = #"";
Include the header wherever you want to use that. i think in your code its crashing because of multiple time definition of labelString variable.

"Receiver type for instance messages is a forward declaration" in xcode 4.6

I want to call c++ class in my ViewController. So I create a class like this:
Hello.h
#import <Foundation/Foundation.h>
#interface Hello : NSObject{
class NewHello{
private:int greeting_text;
public:
NewHello(){
greeting_text=5;
}
void say_hello(){
printf("Greeting_Text = %d",greeting_text);
}
};
NewHello *hello;
}
-(void)sayHellooooo;
#end
Hello.mm
#import "Hello.h"
#implementation Hello
-(void)sayHellooooo{
hello = new NewHello();
hello->say_hello();
}
#end
ViewController.h
#import <UIKit/UIKit.h>
//#include "Hello.h"
#class Hello;
#interface ViewController : UIViewController{
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"dddddddd");
Hello *aa = [[Hello alloc]init];
[aa sayHellooooo];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
It works fine in the project:http://files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar
But when I copy the code to my project,it appears "Receiver type for instance messages is a forward declaration" error.
If I change "#class Hello;" to #import "Hello.h",it appears "Unkwon type class,did you mean Class" error in "class NewHello".
I use xcode 4.6.Can anybody help me?Thank you!
The problem is (as you've stated) file type for ViewController.m is Obj-C and Hello.h is a Obj-C++ file. The solution is to add
#import "Hello.h"
to your ViewController.m file and change the file type of ViewController.m to Obj-C++ (from the right panel)

Resources